title: Android Textwatcher java.lang.NullPointerException error. Unable to initialize event listener
body: <p>I'm trying to use a TextWatcher with an EditText in my activity. I've written the class for the listener, my code compiles without error. </p>
  
  <p>But at run time it crashes when I attempt to add the listener to the EditText. Giving a nullPointerException. I believe I've narrowed the error down to this line:</p>
  
  <pre><code>    fatherBox.addTextChangedListener(new EditTextWatcher());
  </code></pre>
  
  <p>I really can't see any errors with my code, the EditText does exist. I'm even starting to wonder if its a problem with Android Studio.</p>
  
  <p>On create method:</p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_new_clutch);
  
      if (savedInstanceState == null) {
          getSupportFragmentManager().beginTransaction()
                  .add(R.id.container, new PlaceholderFragment())
                  .commit();
      }
  
      EditText        fatherBox = (EditText) findViewById(R.id.clutch_father_box);
      fatherBox.addTextChangedListener(new EditTextWatcher());
  
  //end on create
  }
  </code></pre>
  
  <p>Listener object: </p>
  
  <pre><code>private class EditTextWatcher implements TextWatcher {
  
      public void afterTextChanged(Editable s) {
  
  
      }
  
      public void beforeTextChanged(CharSequence s, int start, int count,
                                    int after) {
      }
  
      public void onTextChanged(CharSequence s, int start, int before, int count) {
          Context context = getApplicationContext();
          CharSequence text = "Hello toast!";
          int duration = Toast.LENGTH_SHORT;
  
          Toast toast = Toast.makeText(context, text, duration);
          toast.show();
      }
  
  }
  </code></pre>
  
  <p>Activity snippet</p>
  
  <pre><code>&lt;EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:inputType="textPersonName"
      android:ems="10"
      android:id="@id/clutch_father_box"
      android:layout_below="@id/clutch_temp_box"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"
      android:layout_alignLeft="@id/clutch_temp_box" /&gt;
  </code></pre>