title: NullPointerException: Attempt to invoke virtual method enableForegroundDispatch(...) on a null object reference
body: <p>I'm developing a sample Android app that has two activities</p>
  
  <ol>
  <li>A log in page</li>
  <li>An activity that writes NDEF message to an NFC tag.</li>
  </ol>
  
  <p>Upon tapping the log in button, I get the NullPointerException. Here is the entire log:</p>
  
  <pre><code>12-08 11:57:32.510: E/AndroidRuntime(1162): FATAL EXCEPTION: main
  12-08 11:57:32.510: E/AndroidRuntime(1162): Process: com.example.garageconnect, PID: 1162
  12-08 11:57:32.510: E/AndroidRuntime(1162): java.lang.RuntimeException: Unable to resume activity {com.example.garageconnect/com.example.garageconnect.WriteActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.nfc.NfcAdapter.enableForegroundDispatch(android.app.Activity, android.app.PendingIntent, android.content.IntentFilter[], java.lang.String[][])' on a null object reference
  12-08 11:57:32.510: E/AndroidRuntime(1162):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3103)
  12-08 11:57:32.510: E/AndroidRuntime(1162):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)
  12-08 11:57:32.510: E/AndroidRuntime(1162):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)
  12-08 11:57:32.510: E/AndroidRuntime(1162):     at android.app.ActivityThread.-wrap11(ActivityThread.java)
  12-08 11:57:32.510: E/AndroidRuntime(1162):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
  12-08 11:57:32.510: E/AndroidRuntime(1162):     at android.os.Handler.dispatchMessage(Handler.java:102)
  12-08 11:57:32.510: E/AndroidRuntime(1162):     at android.os.Looper.loop(Looper.java:148)
  12-08 11:57:32.510: E/AndroidRuntime(1162):     at android.app.ActivityThread.main(ActivityThread.java:5417)
  12-08 11:57:32.510: E/AndroidRuntime(1162):     at java.lang.reflect.Method.invoke(Native Method)
  12-08 11:57:32.510: E/AndroidRuntime(1162):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
  12-08 11:57:32.510: E/AndroidRuntime(1162):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  12-08 11:57:32.510: E/AndroidRuntime(1162): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.nfc.NfcAdapter.enableForegroundDispatch(android.app.Activity, android.app.PendingIntent, android.content.IntentFilter[], java.lang.String[][])' on a null object reference
  12-08 11:57:32.510: E/AndroidRuntime(1162):     at com.example.garageconnect.WriteActivity.WriteModeOn(WriteActivity.java:134)
  12-08 11:57:32.510: E/AndroidRuntime(1162):     at com.example.garageconnect.WriteActivity.onResume(WriteActivity.java:129)
  12-08 11:57:32.510: E/AndroidRuntime(1162):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1258)
  12-08 11:57:32.510: E/AndroidRuntime(1162):     at android.app.Activity.performResume(Activity.java:6312)
  12-08 11:57:32.510: E/AndroidRuntime(1162):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092)
  12-08 11:57:32.510: E/AndroidRuntime(1162):     ... 10 more
  </code></pre>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>public class MainActivity extends Activity {
  
      EditText t1,t2;
      Button b1;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          t1=(EditText)findViewById(R.id.editText1);
          t2=(EditText)findViewById(R.id.editText2);
          b1=(Button)findViewById(R.id.button1);
          b1.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  String username=t1.getText().toString(),pwd=t2.getText().toString();
                  if( username.equals("Siva") &amp;&amp; pwd.equals("pwd123")){
                      Intent i = new Intent(getBaseContext(), WriteActivity.class);
                      startActivity(i);   
                 }
              }
          });
      }
  }
  </code></pre>
  
  <p><strong>WriteActivity.java</strong></p>
  
  <pre><code>public class WriteActivity extends Activity {
  
      boolean writeMode;
      NfcAdapter adapter;
      PendingIntent pendingIntent;
      IntentFilter writeTagFilters[];
      EditText t1,t2;
      Button b1;
      String nfcmsg;
      Tag tag;
      Context ctx;
      String str;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.write_activity);
          t1=(EditText)findViewById(R.id.editText1);
          t2=(EditText)findViewById(R.id.editText2);
          b1=(Button)findViewById(R.id.button1);
          ctx=this;
  
          b1.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  try {
                      if(tag==null){
                          Toast.makeText(ctx, "No tag found", Toast.LENGTH_LONG ).show();
                      }
                      else{
                          write(t1.getText().toString(),t2.getText().toString(),tag);
                          Toast.makeText(ctx, "Done Successfully", Toast.LENGTH_LONG ).show();
                      }
                  } 
                  catch (IOException e) {
                      Toast.makeText(ctx, "Error in Writing", Toast.LENGTH_LONG ).show();
                      e.printStackTrace();
                  } 
                  catch (FormatException e) {
                      Toast.makeText(ctx, "Error in Writing FF" , Toast.LENGTH_LONG ).show();
                      e.printStackTrace();
                  }
              }
          });
  
          adapter = NfcAdapter.getDefaultAdapter(this);
          pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
          IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
          tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
          writeTagFilters = new IntentFilter[] { tagDetected };
  
      }
  
      private void write(String locid,String secid, Tag tag) throws IOException, FormatException {
  
          //Code that calls createRecord method to create NDEFRecord and write NDEF message to NFC tag
      }
  
  
  
      private NdefRecord createRecord(String text) throws UnsupportedEncodingException {
          //Code to create NDEF record and returns the record to write method
      }
  
  
      @Override
      protected void onNewIntent(Intent intent){
          if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())){
              tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);    
              Toast.makeText(this, "Tag Detected. Now Write!", Toast.LENGTH_LONG ).show();
          }
      }
  
      //@Override
      public void onPause(){
          super.onPause();
          WriteModeOff();
      }
  
      @Override
      public void onResume(){
          super.onResume();
          WriteModeOn();
      }
  
      private void WriteModeOn(){
          writeMode = true;
          adapter.enableForegroundDispatch(this, pendingIntent, writeTagFilters, null);
      }
  
      private void WriteModeOff(){
          writeMode = false;
          adapter.disableForegroundDispatch(this);
      }
  }
  </code></pre>