-3

I get an exception while trying to write a MifareUltralight NFC tag on Android. The error shown in logcat is:

java.lang.NullPointerException: Attempt to invoke virtual method 'void    android.view.View.setVisibility(int)' on a null object reference at   
    .MainActivity.getTagInfo(MainActivity.java:124) 
    com.mynfctest.MainActivity.resolveIntent(MainActivity.java:106)
    com.mynfctest.MainActivity.onNewIntent(MainActivity.java:98) 

How I can solve this error?

Write method:

public static boolean writeOnMifareUltralight(Context _context, Tag tag, String pageData, int i) {
    MifareUltralight mifare = null;

    int size=pageData.length();
    try {
        mifare = MifareUltralight.get(tag);
        mifare.connect();
        mifare.writePage(i, pageData.getBytes(Charset.forName("US-ASCII")));
    } catch (Exception ex) {
        ex.printStackTrace();
        Log.d("skm", ex.getMessage());
        // return false;
    } finally {
        try {
            mifare.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    return true;
}

Write method called from my activity:

switch (mifareUlTag.getType()) {
    case MifareUltralight.TYPE_ULTRALIGHT:
        boolean result=NFCHammer.writeOnMifareUltralight(this,tag,tvName.getText().toString(),4);

        if(result){
            findViewById(R.id.incProgressBar).setVisibility(View.GONE);
            Intent Callintent = new Intent(this, HomeActivity.class);
            Callintent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(Callintent);
            finish();
            overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
        }
        else{
            findViewById(R.id.incProgressBar).setVisibility(View.GONE);
            CommonTask.createToast("Tap The card again!!!", this, Color.RED);
        }
        break;
}
Michael Roland
  • 39,663
  • 10
  • 99
  • 206
JOKEY
  • 21
  • 5
  • I got wirte exception mifare.writePage(i, pageData.getBytes(Charset.forName("US-ASCII"))); boolean result=NFCHammer.writeOnMifareUltralight(this,tag,tvName.getText().toString(),4); – JOKEY Dec 08 '15 at 06:58
  • logcat error-- java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setVisibility(int)' on a null object reference at .MainActivity.getTagInfo(MainActivity.java:124) com.mynfctest.MainActivity.resolveIntent(MainActivity.java:106) .com.mynfctest.MainActivity.onNewIntent(MainActivity.java:98) – JOKEY Dec 08 '15 at 07:11
  • Edit your post, don't comment your logcat here. – Razgriz Dec 08 '15 at 07:17
  • thanks, why any body unvote my question. I want to know that .please help – JOKEY Dec 08 '15 at 07:26
  • It seems your error in logcat has nothing to do with nfc. You have an error in class MainActivity line 124, you call setVisibility on a null object – LaurentY Dec 08 '15 at 09:11

1 Answers1

0

As the exception explains, you are calling the method setVisibility on a null ob ject reference:

java.lang.NullPointerException: Attempt to invoke virtual method '... setVisibility(int)' on a null object reference

In other words, somewhere in your code, you have a like this:

object.setVisibility(...);

and on this like the variable object is null and does not reference a real object.

The stacktrace of the exception further tells you that the exception occured on line 124 of the file MainActivity.java, more specifically inside a method named getTagInfo:

at .MainActivity.getTagInfo(MainActivity.java:124)

As you did not reveal enough code to verify if the problem comes from those code snippets that you posted, we can only speculate that the problem comes from the two lines that set the visibility of a view R.id.incProgressBar to GONE:

findViewById(R.id.incProgressBar).setVisibility(View.GONE);

If that's the case, then findViewById(R.id.incProgressBar) returned null, indicating that the view R.id.incProgressBar was not found in the current view hiearchy of the activity.

Michael Roland
  • 39,663
  • 10
  • 99
  • 206