简体   繁体   中英

android variable null pointer

I think the variable I'm checking is null. My on resume method does this:

This method checks for an email on the web and will return -1 if something goes wrong. I thought this would work. Now when I ran it 10 times it was working. So I gave it to a friend. He says it gives him that exception sometimes.

///   Ok found it it was in a try catch in the OnCreate() 

tvEmail = (TextView) findViewById(R.id.itemz_email);
        tvEmail.setTypeface(TitleFont);

///  and occasionally it didn't even get initialized.
///  I guess ill just init it again in the onResume() just in case.
///  Thanks!


@Override
protected void onResume() {
    super.onResume();
    tvEmail = (TextView) findViewById(R.id.itemz_email);
        tvEmail.setTypeface(TitleFont);

    if (EMAIL != null) {
        tvEmail.setText("E-mail: " + EMAIL);
    } else {
        tvEmail.setText("E-mail: No Email Available");  //<----==LINE 77
    }
}

This is the error my user is getting:

 java.lang.RuntimeException: Unable to resume activity {com.codalata.craigslistchecker/com.codalata.craigslistchecker.ItemView}: java.lang.NullPointerException
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2578)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2606)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2092)
    at android.app.ActivityThread.access$600(ActivityThread.java:133)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1198)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4777)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
    at com.codalata.craigslistchecker.ItemView.onResume(ItemView.java:77)<------/**/error/**/
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1184)
    at android.app.Activity.performResume(Activity.java:5082)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2568)

Should I just check and set it like this?

if(EMAIL == null){
    EMAIL = "E-mail: No Email Available"
    tvEmail.setText(EMAIL);
}else {
    tvEmail.setText("E-mail: " + EMAIL);
}

You're getting a NullReferenceException on the line containing tvEmail.setText(...) ; swapping your conditional blocks to check EMAIL for null vs. not isn't going to change things, since EMAIL isn't getting involved on the line causing the error.

You need to look at tvEmail , because that is what's being null at the time your error occurs.

Since we know that the null pointer exception is coming from line 77:

tvEmail.setText("E-mail: No Email Available");  //<----==LINE 77

We know that the string cannot be null since it is defined in quotes. the method setText can not be null since methods are not objects and can not be null!. That leaves us to the tvEmail object which must be null leading to a null pointer exception. Please ensure that this object was initialized

 tvEmail = new TvEmail();//or something similar

To make sure that this object is null (to check we are doing the right solution) insert this line of code after

 super.onResume();

Insert this:

if(tvEmail == null) 
    System.out.println("tvEmail is null!!!);

If tvEmail is null you may either want to recrate the object or set it equal to something like this:

 tvEmail = (TextView)findViewById(id);

First check you declared your object in onCreate method from Activity or Fragment.etc.. And if you want to check that the object is not null when the onResume method is called you it should looks like this. Remember it will be null in else because you said if that its not null do that, else its null set email not available but the problem is that in else the variable doesnt have a reference in others words its null, you should also check why your variable its null at that point.

if (EMAIL != null) {
    tvEmail.setText("E-mail: " + EMAIL);
} else {
    tvEmail = (TextView)findViewById(id);
    tvEmail.setText("E-mail: No Email Available");
}

Edited

This is not the best option you should go because you'll create a new object on onResume everytime it's called. You should check why the variable is null before onResume is called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM