title: Why do I get null pointer exception trying to access EditText data?
body: <p>My application has several EditText boxes, and when the "done" button is pressed I validate the input and return the activity result.  The problem is that when I try to read the data it is null, and I can't figure out why.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>public class CreatePlayer extends Activity implements OnItemSelectedListener {
  
  private EditText textFirstName;
  private EditText textLastName;
  private EditText jerseyNumber;
  private Spinner spinner;
  
  private int arrayID;
  
  private boolean validFirstName = false;
  private boolean validLastName = false;
  private boolean validJerseyNumber = false;
  private boolean validPosition = false;
  
  private static final String TAG = "CreatePlayer";
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.createplayer);
  
      final EditText textFirstName = (EditText) findViewById(R.id.playername_first_edit);
      EditText textLastName = (EditText) findViewById(R.id.playername_last_edit);
      final EditText jerseyNumber = (EditText) findViewById(R.id.playernum_edit);
  
      arrayID = getIntent().getExtras().getInt("arrayID");
      Spinner spinner = (Spinner) findViewById(R.id.spinner);
      ArrayAdapter&lt;CharSequence&gt; adapter = ArrayAdapter.createFromResource(
              this, arrayID, android.R.layout.simple_spinner_item);
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      spinner.setAdapter(adapter);
      spinner.setOnItemSelectedListener(this);
  
      textFirstName
              .setOnEditorActionListener(new EditText.OnEditorActionListener() {
                  @Override
                  public boolean onEditorAction(TextView v, int actionId,
                          KeyEvent event) {
                      if (actionId == EditorInfo.IME_ACTION_DONE) {
  
                          boolean result = v
                                  .getText()
                                  .toString()
                                  .matches(
                                          getString(R.string.regex_first_name));
  
                          System.out.println(v.getText());
                          System.out.println(result);
                          Pattern p = Pattern
                                  .compile(getString(R.string.regex_first_name));
                          Matcher m = p.matcher(v.getText());
                          Log.v(TAG, "pattern=" + p.toString());
                          Log.v(TAG, "text=" + v.getText());
                          Log.v(TAG, "result=" + result);
                          Log.v(TAG, "find=" + m.find());
  
                          if (result) {
                              validFirstName = true;
                              //textFirstName = (EditText) v.getText();
                          }
  
                      }
                      return false;
                  }
              });
      textLastName
              .setOnEditorActionListener(new EditText.OnEditorActionListener() {
                  @Override
                  public boolean onEditorAction(TextView v, int actionId,
                          KeyEvent event) {
                      if (actionId == EditorInfo.IME_ACTION_DONE) {
                          boolean result = v
                                  .getText()
                                  .toString()
                                  .matches(
                                          getString(R.string.regex_last_name));
                          Pattern p = Pattern
                                  .compile(getString(R.string.regex_last_name));
                          Matcher m = p.matcher(v.getText());
                          Log.v(TAG, "pattern=" + p.toString());
                          Log.v(TAG, "text=" + v.getText());
                          Log.v(TAG, "result=" + result);
                          Log.v(TAG, "find=" + m.find());
  
                          if (result) {
                              validLastName = true;
                          }
                      }
                      return false;
                  }
              });
      jerseyNumber
              .setOnEditorActionListener(new EditText.OnEditorActionListener() {
                  @Override
                  public boolean onEditorAction(TextView v, int actionId,
                          KeyEvent event) {
                      if (actionId == EditorInfo.IME_ACTION_DONE) {
  
                          Pattern p = Pattern
                                  .compile(getString(R.string.regex_jerseynum));
                          boolean result = v
                                  .getText()
                                  .toString()
                                  .matches(
                                          getString(R.string.regex_jerseynum));
                          Matcher m = p.matcher(v.getText());
                          Log.v(TAG, "pattern=" + p.toString());
                          Log.v(TAG, "text=" + v.getText());
                          Log.v(TAG, "result=" + result);
                          Log.v(TAG, "find=" + m.find());
  
                          if (result) {
                              validJerseyNumber = true;
                          }
                      }
                      return false;
                  }
              });
  }
  
  public void okClicked(View v) {
  
      PlayerInfo newPlayer;
  
      if (validFirstName &amp;&amp; validLastName &amp;&amp; validJerseyNumber
              &amp;&amp; validPosition) {
          Log.v(TAG, "JerseyNumber = " + jerseyNumber);
          // jerseyNumber is null???
          int num = Integer.parseInt(jerseyNumber.getText().toString());
          String type = spinner.getSelectedItem().toString();
          Log.v(TAG, "type = " + type);
          String name = textFirstName.getText().toString() + " "
                  + textLastName.getText().toString();
          newPlayer = new PlayerInfo(BaseBoard.playerIDCounter, num, type,
                  name);
          Intent result = new Intent();
          result.putExtra(getString(R.string.players_create), newPlayer);
          setResult(5, result);
          this.finish();
      } else {
          Toast toast = Toast.makeText(getApplicationContext(),
                  "Invalid input. Try something like John Smith, #27, PG",
                  Toast.LENGTH_SHORT);
          toast.show();
      }
  }
  
  public void cancelClicked(View v) {
      this.setResult(-1, null);
      this.finish();
  }
  
  @Override
  public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int pos,
          long id) {
      validPosition = true;
      System.out.println(parent.getItemAtPosition(pos).toString());
  }
  
  @Override
  public void onNothingSelected(AdapterView&lt;?&gt; parent) {
  }
  </code></pre>