0

I keep getting a fatal error saying nullpointer exception on String action = intent.getAction();

How do I get it to work ? I want to transfer a text file to another device using Android Beam. The beam detects it and launches the 'tap to beam' on the phone but never appears to complete the beam.

           @TargetApi(16)
        public void sendNfcFile(){
            nfcAdapter = NfcAdapter.getDefaultAdapter(this);

            if(!nfcAdapter.isEnabled()){
                Toast.makeText(this, "Please enbable NFC in settings", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(Settings.ACTION_NFC_SETTINGS));
            }else if(!nfcAdapter.isNdefPushEnabled()){
                Toast.makeText(this, "Please enable Android Beam", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS));
            }else{
                String filename = "personalShare.txt";
                File fileToTransfer = Environment.getExternalStoragePublicDirectory(filename);

                fileToTransfer.setReadable(true, false);

               nfcAdapter.setBeamPushUris(new Uri[]{Uri.fromFile(fileToTransfer)}, this);
            }
        }

        @Override
        protected void onNewIntent(Intent intent) {
            setIntent(intent);

        }
        @Override
        protected void onResume() {
            super.onResume();
            Intent intent = getIntent();
            String action = intent.getAction();
            if(action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED)){
                Parcelable[] parcelables =
                        intent.getParcelableArrayExtra(
                                NfcAdapter.EXTRA_NDEF_MESSAGES);
                NdefMessage inNdefMessage = (NdefMessage)parcelables[0];
                NdefRecord[] inNdefRecords = inNdefMessage.getRecords();
                NdefRecord NdefRecord_0 = inNdefRecords[0];
                String inMsg = new String(NdefRecord_0.getPayload());
                TextView name  = (TextView) findViewById(R.id.fullname);
                name.setText(inMsg);
            }
        }
Hazed 2.0
  • 133
  • 2
  • 14
  • `a fatal error saying nullpointer exception on Intent intent = getIntent().`. That i do not believe. But i can imagine that getIntent() delivers null. So check before use. And further you should google for `NullPointerException`. – greenapps Apr 05 '18 at 09:26
  • yeah sorry, you're right it's String action = intent.getAction(); that the null pointer exception is showing – Hazed 2.0 Apr 05 '18 at 16:44

0 Answers0