title: Java Switch statement throwing Java.lang.NullPointerException - Android Beginner
body: <p>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?</p>
  
  <p>Code:</p>
  
  <pre><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&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(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&lt;?&gt; 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;
      }*/
  
  }
  </code></pre>
  
  <p>and then here is my logcat:</p>
  
  <pre><code>    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)
  </code></pre>
  
  <p>Thanks in advance!</p>