title: Attempt to invoke virtual method 'void android.nfc.tech.MifareClassic.connect()' on a null object reference
body: <p>I've been developing an application in Android Studio to read and write NFC tags, specifically Mifare Classic tags. I managed to develop and test it on my smartphone (with S.O. KitKat) in early 2016 (a year ago).</p>
  
  <p>As I mentioned, leave aside the application, and after having updated the version of Android Studio, the SDK and the S.O. From my smartphone to MarshMallow, this error appears when trying to write to the label: "java.lang.NullPointerException: Attempt to invoke virtual method 'void android.nfc.tech.MifareClassic.connect()' on a null object reference".</p>
  
  <p>This error is apparently generated when trying to connect to MifareClassic tag.</p>
  
  <p>Attached the code of my activity replacing some parts with ... that I consider are irrelevant.</p>
  
  <pre><code>import android.annotation.SuppressLint;
  import android.app.Activity;
  
  import android.app.PendingIntent;
  import android.content.Context;
  import android.content.Intent;
  import android.content.IntentFilter;
  import android.nfc.FormatException;
  import android.nfc.NdefMessage;
  import android.nfc.NdefRecord;
  import android.nfc.NfcAdapter;
  import android.nfc.Tag;
  import android.nfc.tech.MifareClassic;
  import android.nfc.tech.Ndef;
  import android.nfc.tech.NdefFormatable;
  import android.os.Bundle;
  import android.view.View.OnClickListener;
  ...
  
  import java.io.IOException;
  import java.io.UnsupportedEncodingException;
  
  @SuppressLint("Escribir")
  public class escribir extends Activity {
      NfcAdapter adapter;
      PendingIntent pendingIntent;
      IntentFilter writeTagFilters[];
      boolean writeMode;
      Tag myTag;
      MifareClassic mfc;
      Context context;
      ...
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.activity_datospropietario);
          context = this;
          ...
  
          Button btnWrite = (Button)findViewById(R.id.button);
          final String b = getIntent().getExtras().getString("datos");
  
          btnWrite.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  final String mensaje = (b + ...);
  
                  if (first.getText().toString().isEmpty()) {
                      Toast.makeText(context, context.getString(R.string.missing_fields), Toast.LENGTH_SHORT).show();
                  } else {
                      if (myTag == null) {
                          Toast.makeText(context, context.getString(R.string.error_notag), Toast.LENGTH_LONG).show();
                      } else {
                      MifareClassic tmpMFC = null;
                      try {
                          tmpMFC = MifareClassic.get(myTag);
                      } catch (Exception e) {
                          Toast.makeText(context, context.getString(R.string.error_notag), Toast.LENGTH_LONG).show();
                          e.printStackTrace();
                      }
                      mfc = tmpMFC;
  
                      int sect;
                      if (mfc != null) {
                          sect = mfc.getSectorCount();
                      }
                      try {
                          mfc.connect();
                          ...
                      } catch (IOException e) {
                          Toast.makeText(context, context.getString(R.string.error_notag), Toast.LENGTH_LONG).show();
                          e.printStackTrace();
                          myTag = null; 
                      }
                  }
              }
          });
  
          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 text, Tag tag, int sector) throws IOException, FormatException {
  
          NdefRecord[] records = {createRecord(text), NdefRecord.createApplicationRecord("my_app")};
          NdefMessage mensaje = new NdefMessage(records);
  
          NdefFormatable formatable = NdefFormatable.get(tag);
  
          if (formatable != null) {
              formatable.connect();
              formatable.format(mensaje);
              formatable.close();
          } else {
              Ndef ndef = Ndef.get(tag);
              ndef.connect();
              ndef.writeNdefMessage(mensaje);
              ndef.close();
          }
  
          MifareClassic mfc = MifareClassic.get(tag);
          ...
      }
  
      @SuppressLint("Escribir") private NdefRecord createRecord(String text) throws UnsupportedEncodingException{
          String lang = "es";
          byte[] textBytes = text.getBytes();
          byte[] langBytes = lang.getBytes("US-ASCII");
          int langLength = langBytes.length;
          int textLength = textBytes.length;
          byte[] payLoad = new byte[1 + langLength + textLength];
  
          payLoad[0] = (byte) langLength;
  
          System.arraycopy(langBytes, 0, payLoad, 1, langLength);
          System.arraycopy(textBytes, 0, payLoad, 1 + langLength, textLength);
  
          return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], payLoad);
      }
  
      @SuppressLint("Escribir") protected void onNewIntent(Intent intent){
          if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
              myTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
          }
      }
  
      public void onPause(){
          super.onPause();
          WriteModeOff();
      }
      public void onResume(){
          super.onResume();
          WriteModeOn();
      }
  
      @SuppressLint("Escribir") private void WriteModeOn(){
          writeMode = true;
          adapter.enableForegroundDispatch(this, pendingIntent, writeTagFilters, null);
      }
  
      @SuppressLint("Escribir") private void WriteModeOff(){
          writeMode = false;
          adapter.disableForegroundDispatch(this);
      }
  }
  </code></pre>