title: Android NFC - Attempt to invoke virtual method 'boolean android.nfc.Tag.hasTech(int)' on a null object reference
body: <p>I am trying to read smartcards via NFC on Android. I had a solution working in Eclipse, but after migrating to Android Studio it only works once, reads all the data from the card, but then freezes and repeats this error over and over:</p>
  <blockquote>
  <p>Attempt to invoke virtual method 'boolean android.nfc.Tag.hasTech(int)' on a null object reference</p>
  <p>W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.nfc.Tag.hasTech(int)' on a null object reference</p>
  <p>W/System.err:     at android.nfc.tech.IsoDep.get(IsoDep.java:61)</p>
  </blockquote>
  <p>AndroidManifest.xml:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      package=&quot;packageName&quot;
      android:versionCode=&quot;1&quot;
      android:versionName=&quot;1.0&quot; &gt;
  
      &lt;uses-sdk
          android:minSdkVersion=&quot;17&quot;
          android:targetSdkVersion=&quot;23&quot; /&gt;
  
      &lt;uses-permission android:name=&quot;android.permission.NFC&quot; /&gt;
  
      &lt;uses-feature
          android:name=&quot;android.hardware.nfc&quot;
          android:required=&quot;false&quot; /&gt;
  
      &lt;application
          android:allowBackup=&quot;true&quot;
          android:label=&quot;@string/app_name&quot;
          android:supportsRtl=&quot;true&quot; &gt;
  
          &lt;activity
              android:name=&quot;.NFCReader&quot;
              android:label=&quot;@string/app_name&quot;
              android:launchmode=&quot;singleTask&quot; &gt;
  
              &lt;intent-filter&gt;
                  &lt;action android:name=&quot;android.nfc.action.TECH_DISCOVERED&quot; /&gt;
              &lt;/intent-filter&gt;
  
              &lt;meta-data
                  android:name=&quot;android.nfc.action.TECH_DISCOVERED&quot;
                  android:resource=&quot;@xml/nfc_tech_filter&quot; /&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  <p>NFCReader:</p>
  <pre><code>public class NFCReader extends Activity {
  
  private NfcAdapter mNfcAdapter;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
  
      if (mNfcAdapter == null) {
          finish();
          return;
      }
  
      if (!mNfcAdapter.isEnabled()) {
          Log.e(TAG, &quot;NFC is disabled.&quot;);
      } else {
      }
  
      final Tag tagFromIntent = this.getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
      new ReadCardTask().execute(tagFromIntent);
  
      handleIntent(getIntent());
      this.finish();
  }
  
  private void handleIntent(Intent intent) {
      String action = intent.getAction();
  
      if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
          String type = intent.getType();
          if (MIME_TEXT_PLAIN.equals(type)) {
              Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
              new NdefReaderTask().execute(tag);
          } else {
              Log.d(TAG, &quot;Wrong mime type: &quot; + type);
          }
      } else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
          Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
          String[] techList = tag.getTechList();
          String searchedTech = Ndef.class.getName();
  
          for (String tech : techList) {
              if (searchedTech.equals(tech)) {
                  new NdefReaderTask().execute(tag);
                  break;
              }
          }
      }
  }
  
  protected Void doInBackground(Tag... params) {
      IsoDep tag = IsoDep.get(params[0]);
      tag.connect();
      // [...] transmit APDUs
  }
  
  }
  </code></pre>