Question

So i want to read from a Tag, but get an NullPointerException when connecting to it. I know that my Tag is not null, as my Activity starts upon detecting it and an additional Test in the Code proves it.

public void getDataFromTag()
{
    tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    text.setText(tag.toString());
    text.append("\n\n");
    // get NDEF tag details
    MifareClassic mfc = MifareClassic.get(tag);
    byte[] data;
    if (tag == null)
        text.append("\n Tag is null");
    try
    {
        mfc.connect();
        ...
    }
    catch (IOException e) 
    { 
        text.append("\n " + "Error while reading!");
    } 
    catch (NullPointerException e)
    {
        text.append("\n " + e.toString());
    }
}

So what is the problem here? Btw, I am testing my app on a Nexus 7 Tablet.

On popular demand... here's a logCat

04-13 15:56:47.101: D/libEGL(2046): loaded /system/lib/egl/libEGL_tegra.so
04-13 15:56:47.121: D/libEGL(2046): loaded /system/lib/egl/libGLESv1_CM_tegra.so
04-13 15:56:47.131: D/libEGL(2046): loaded /system/lib/egl/libGLESv2_tegra.so
04-13 15:56:47.151: D/OpenGLRenderer(2046): Enabling debug mode 0
04-13 15:56:52.161: D/AndroidRuntime(2046): Shutting down VM
04-13 15:56:52.161: W/dalvikvm(2046): threadid=1: thread exiting with uncaught exception (group=0x41037930)
04-13 15:56:52.171: E/AndroidRuntime(2046): FATAL EXCEPTION: main
04-13 15:56:52.171: E/AndroidRuntime(2046): java.lang.RuntimeException: Unable to resume activity {bsp.app/bsp.app.Main_Menue}: java.lang.NullPointerException
04-13 15:56:52.171: E/AndroidRuntime(2046):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2742)
04-13 15:56:52.171: E/AndroidRuntime(2046):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2771)
04-13 15:56:52.171: E/AndroidRuntime(2046):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2235)
04-13 15:56:52.171: E/AndroidRuntime(2046):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-13 15:56:52.171: E/AndroidRuntime(2046):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-13 15:56:52.171: E/AndroidRuntime(2046):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-13 15:56:52.171: E/AndroidRuntime(2046):     at android.os.Looper.loop(Looper.java:137)
04-13 15:56:52.171: E/AndroidRuntime(2046):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-13 15:56:52.171: E/AndroidRuntime(2046):     at java.lang.reflect.Method.invokeNative(Native Method)
04-13 15:56:52.171: E/AndroidRuntime(2046):     at java.lang.reflect.Method.invoke(Method.java:511)
04-13 15:56:52.171: E/AndroidRuntime(2046):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-13 15:56:52.171: E/AndroidRuntime(2046):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-13 15:56:52.171: E/AndroidRuntime(2046):     at dalvik.system.NativeStart.main(Native Method)
04-13 15:56:52.171: E/AndroidRuntime(2046): Caused by: java.lang.NullPointerException
04-13 15:56:52.171: E/AndroidRuntime(2046):     at bsp.app.Main_Menue.getDataFromTag(Main_Menue.java:81)
04-13 15:56:52.171: E/AndroidRuntime(2046):     at bsp.app.Main_Menue.onResume(Main_Menue.java:65)
04-13 15:56:52.171: E/AndroidRuntime(2046):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1185)
04-13 15:56:52.171: E/AndroidRuntime(2046):     at android.app.Activity.performResume(Activity.java:5182)
04-13 15:56:52.171: E/AndroidRuntime(2046):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2732)
04-13 15:56:52.171: E/AndroidRuntime(2046):     ... 12 more
Was it helpful?

Solution

You do not need to get the Tag object to get to the NDEF content, Android reads NDEf content per default and you can get it using

Parcelable[] messages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (messages != null) {
    NdefMessage[] ndefMessages = new NdefMessage[messages.length];
    for (int i = 0; i < messages.length; i++) {
        ndefMessages[i] = (NdefMessage) messages[i];
    }
    // process messages, usually only a single is present
}

Note that getting any particular tag technology class (MifareClassic) does not work if the tag is in fact not of that technology - that is what you are seeing. So you should first check which tag type you have.

OTHER TIPS

You should check whether mfc is not null after the line MifareClassic mfc = MifareClassic.get(tag);. If it is null, the tag is perhaps not MIFARE Classic (perhaps you should try MifareUltralight instead).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top