Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8044555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:16:05+00:00 2026-06-05T05:16:05+00:00

I am a new beginner so please bear with me. I created a spinner

  • 0

I am a new beginner so please bear with me. I created a spinner and populated it with a string array, I then created a the method “OnItemSelected” and inserted a switch statement. The activity then throws a NullPointerException. When I remove the switch statement, the activity works. How can i fix this?

Code:

EditText player1 = null;
EditText player2 = null;
EditText player3 = null;
EditText player4 = null;
EditText player5 = null;
EditText player6 = null;
EditText player7 = null;
EditText player8 = null;

Spinner spinner;

int numberOfPlayers = 0;
int counter = 0;

private String[] players ={"2","3","4", "5", "6", "7", "8"};
//private String[] names ={" "};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try {        
    spinner = (Spinner)findViewById(R.id.spinnerplayers);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, players);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

    spinner.setOnItemSelectedListener(this);
    }catch (Exception e) {

        // handle any errors

        Log.e("TestActivity", "Error in activity", e);  // log the error

        // Also let the user know something went wrong

        Toast.makeText(getApplicationContext(), e.getClass().getName() + " " + e.getMessage(),Toast.LENGTH_LONG).show();
    }
}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
        long id) {
        numberOfPlayers = pos + 2;

    /*switch(pos){
    case 0:
        player1.findViewById(R.id.editTextPlayer1);
        player2.findViewById(R.id.editTextPlayer2);

        player1.setVisibility(View.VISIBLE);
        player2.setVisibility(View.VISIBLE);
        break;
    case 1:
        player1.findViewById(R.id.editTextPlayer1);
        player2.findViewById(R.id.editTextPlayer2);
        player3.findViewById(R.id.editTextPlayer3);

        player1.setVisibility(View.VISIBLE);
        player2.setVisibility(View.VISIBLE);
        player3.setVisibility(View.VISIBLE);
        break;
    case 2:
        player1.findViewById(R.id.editTextPlayer1);
        player2.findViewById(R.id.editTextPlayer2);
        player3.findViewById(R.id.editTextPlayer3);
        player4.findViewById(R.id.editTextPlayer4);

        player1.setVisibility(View.VISIBLE);
        player2.setVisibility(View.VISIBLE);
        player3.setVisibility(View.VISIBLE);
        player4.setVisibility(View.VISIBLE);
        break;
    case 3: 
        player1.findViewById(R.id.editTextPlayer1);
        player2.findViewById(R.id.editTextPlayer2);
        player3.findViewById(R.id.editTextPlayer3);
        player4.findViewById(R.id.editTextPlayer4);
        player5.findViewById(R.id.editTextPlayer5);

        player1.setVisibility(View.VISIBLE);
        player2.setVisibility(View.VISIBLE);
        player3.setVisibility(View.VISIBLE);
        player4.setVisibility(View.VISIBLE);
        player5.setVisibility(View.VISIBLE);
        break;
    case 4:
        player1.findViewById(R.id.editTextPlayer1);
        player2.findViewById(R.id.editTextPlayer2);
        player3.findViewById(R.id.editTextPlayer3);
        player4.findViewById(R.id.editTextPlayer4);
        player5.findViewById(R.id.editTextPlayer5);
        player6.findViewById(R.id.editTextPlayer6);

        player1.setVisibility(View.VISIBLE);
        player2.setVisibility(View.VISIBLE);
        player3.setVisibility(View.VISIBLE);
        player4.setVisibility(View.VISIBLE);
        player5.setVisibility(View.VISIBLE);
        player6.setVisibility(View.VISIBLE);
        break;
    case 5:
        player1.findViewById(R.id.editTextPlayer1);
        player2.findViewById(R.id.editTextPlayer2);
        player3.findViewById(R.id.editTextPlayer3);
        player4.findViewById(R.id.editTextPlayer4);
        player5.findViewById(R.id.editTextPlayer5);
        player6.findViewById(R.id.editTextPlayer6);
        player7.findViewById(R.id.editTextPlayer7);

        player1.setVisibility(View.VISIBLE);
        player2.setVisibility(View.VISIBLE);
        player3.setVisibility(View.VISIBLE);
        player4.setVisibility(View.VISIBLE);
        player5.setVisibility(View.VISIBLE);
        player6.setVisibility(View.VISIBLE);
        player7.setVisibility(View.VISIBLE);
        break;
    case 6:
        player1.findViewById(R.id.editTextPlayer1);
        player2.findViewById(R.id.editTextPlayer2);
        player3.findViewById(R.id.editTextPlayer3);
        player4.findViewById(R.id.editTextPlayer4);
        player5.findViewById(R.id.editTextPlayer5);
        player6.findViewById(R.id.editTextPlayer6);
        player7.findViewById(R.id.editTextPlayer7);
        player8.findViewById(R.id.editTextPlayer8);

        player1.setVisibility(View.VISIBLE);
        player2.setVisibility(View.VISIBLE);
        player3.setVisibility(View.VISIBLE);
        player4.setVisibility(View.VISIBLE);
        player5.setVisibility(View.VISIBLE);
        player6.setVisibility(View.VISIBLE);
        player7.setVisibility(View.VISIBLE);
        player8.setVisibility(View.VISIBLE);
        break;
    }*/

}

and then here is my logcat:

    06-06 12:44:42.433: E/AndroidRuntime(5764): FATAL EXCEPTION: main
06-06 12:44:42.433: E/AndroidRuntime(5764): java.lang.NullPointerException
06-06 12:44:42.433: E/AndroidRuntime(5764):     at com.brightdesign.truthordare.Main.onItemSelected(Main.java:69)
06-06 12:44:42.433: E/AndroidRuntime(5764):     at android.widget.AdapterView.fireOnSelected(AdapterView.java:873)
06-06 12:44:42.433: E/AndroidRuntime(5764):     at android.widget.AdapterView.access$300(AdapterView.java:45)
06-06 12:44:42.433: E/AndroidRuntime(5764):     at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:839)
06-06 12:44:42.433: E/AndroidRuntime(5764):     at android.os.Handler.handleCallback(Handler.java:587)
06-06 12:44:42.433: E/AndroidRuntime(5764):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-06 12:44:42.433: E/AndroidRuntime(5764):     at android.os.Looper.loop(Looper.java:123)
06-06 12:44:42.433: E/AndroidRuntime(5764):     at android.app.ActivityThread.main(ActivityThread.java:4627)
06-06 12:44:42.433: E/AndroidRuntime(5764):     at java.lang.reflect.Method.invokeNative(Native Method)
06-06 12:44:42.433: E/AndroidRuntime(5764):     at java.lang.reflect.Method.invoke(Method.java:521)
06-06 12:44:42.433: E/AndroidRuntime(5764):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-06 12:44:42.433: E/AndroidRuntime(5764):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-06 12:44:42.433: E/AndroidRuntime(5764):     at dalvik.system.NativeStart.main(Native Method)

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T05:16:07+00:00Added an answer on June 5, 2026 at 5:16 am

    I assume this causes the problems in your code within the onItemSelected()-method:

    player1.findViewById(R.id.editTextPlayer1);
    player2.findViewById(R.id.editTextPlayer2);
    //...
    

    The method findViewById() returns a sub view of another view or of the main view of the activity. In your case the references seem to be Null pointers and you also do not handle the result, which would be the only reason to use the method.

    You should set these references only once in your onCreate()-method:

    spinner = (Spinner)findViewById(R.id.spinnerplayers);
    //...
    player1 = (EditText)findViewById(R.id.editTextPlayer1);
    player2 = (EditText)findViewById(R.id.editTextPlayer2);
    //...
    

    This way all your references are valid after onCreate() and you can access them securely in the onItemSelected() method (which I assume caused the NullPointerException)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am an absolute beginner. So please bear with me if I miss the
I'm completely new to Simple Build Tool, so please pardon my beginner question. I'm
I'm a beginner programmer so please bear with me. This app runs fine but
I'm new to both ajax and jquery, so please excuse the beginner question. I'm
I am just a new beginner in MySQL and I wanted to write the
This might be a beginner question (I'm new to Rails 3), but I'm trying
I'm new to Java and I have a beginner question: NumberFormat is an abstract
I'm fairly new to Rails, and I have a few beginner's questions. I want
This is a beginner-best-practice question in perl. I'm new to this language. The question
I am a beginner in ASP.Net. I am starting a new project using ASP.Net

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.