title: While reading nfc tags i am getting this error?
body: <p>I am trying to make an application which is read <code>text/plain</code> type of tags. But in some times it read well and sometimes it give me this error:</p>
  
  <blockquote>
    <p>java.lang.NullPointerException: Attempt to invoke virtual method
    'android.nfc.NdefRecord[] android.nfc.NdefMessage.getRecords()' on a
    null object reference</p>
  </blockquote>
  
  <pre><code> public class MainActivity extends AppCompatActivity {
  private NfcAdapter mNfcAdapter;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
      setContentView(R.layout.activity_main);
  
      initNFC();
      NFCEnable();
  }
  
  public void NFCEnable ()
  {
      NfcManager manager = (NfcManager) getSystemService(Context.NFC_SERVICE);
      NfcAdapter adapter = manager.getDefaultAdapter();
      if (adapter != null &amp;&amp; adapter.isEnabled()) {
          // adapter exists and is enabled.
      }
      else {
          Toast.makeText(MainActivity.this,"Enable your NFC!",Toast.LENGTH_LONG).show();
      }
  }
  
  private void initNFC(){
      mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
  }
  
  @Override
  protected void onNewIntent(Intent intent) {
      super.onNewIntent(intent);
      Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
  
  
          if(tag != null) {
          Ndef ndef = Ndef.get(tag);
          if(ndef!=null)
          {
              readFromNFC(ndef);
  
          }
          else {
              Toast.makeText(MainActivity.this,"try again!",Toast.LENGTH_LONG).show();
          }
  
      }
      }
  private void readFromNFC(Ndef ndef) {
  
      try {
          ndef.connect();
  
  
              NdefMessage ndefMessage = ndef.getNdefMessage();
              if(ndefMessage.getRecords()[0]!=null &amp;&amp;!ndefMessage.getRecords()[0].getPayload().equals(""))
              {
              String message = new String(ndefMessage.getRecords()[0].getPayload());
  
              Log.e("ok",message);
              if(message.equals("tech"))
              {
                  Intent intent=new Intent(MainActivity.this, Tech_Activation_Activity.class);
                  startActivity(intent);
              }
              else   if(message.equals("event"))
              {
                  Intent intent=new Intent(MainActivity.this, Event_Executed.class);
                  startActivity(intent);
              }
              else {
                  Log.e("ok","blank");
              }
            }
  
          else {
              Toast.makeText(MainActivity.this,"Empty NFC TAG!",Toast.LENGTH_LONG).show();
          }
  
          ndef.close();
      } catch (IOException | FormatException e) {
          e.printStackTrace();
          Log.e("ok",e.toString());
          if(e.toString().equals("java.io.IOException"))
          {
              Toast.makeText(MainActivity.this,"Get your phone near to NFC!",Toast.LENGTH_LONG).show();
          }
  
  
      }
  }
  
  
  @Override
  protected void onResume() {
      super.onResume();
      getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
      IntentFilter techDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
      IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
      IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
      ndefDetected.addCategory(Intent.CATEGORY_DEFAULT);
      try {
          ndefDetected.addDataType("text/plain");
      } catch (IntentFilter.MalformedMimeTypeException e) {
          e.printStackTrace();
      }
      IntentFilter[] nfcIntentFilter = new IntentFilter[]{techDetected,tagDetected,ndefDetected};
  
      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
      if(mNfcAdapter!= null)
          mNfcAdapter.enableForegroundDispatch(this, pendingIntent, nfcIntentFilter, null);
  
  }
  @Override
  protected void onPause() {
      super.onPause();
      if(mNfcAdapter!= null)
          mNfcAdapter.disableForegroundDispatch(this);
  }
  </code></pre>
  
  <p>Please let me know if there is any changes or what I am doing wrong. And also for writing data in <code>nfc</code> tags I am using free application that are <code>available</code> in play store.</p>