简体   繁体   English

Android变量空指针

[英]android variable null pointer

I think the variable I'm checking is null. 我认为我要检查的变量为null。 My on resume method does this: 我的简历方法执行此操作:

This method checks for an email on the web and will return -1 if something goes wrong. 此方法检查网络上的电子邮件,如果出现问题,将返回-1。 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(...) ; 您在包含tvEmail.setText(...)的行上收到NullReferenceException 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. 交换条件块以检查EMAIL是否为null与not不会改变任何事情,因为EMAIL不会参与导致错误的行。

You need to look at tvEmail , because that is what's being null at the time your error occurs. 您需要查看tvEmail ,因为在发生错误时就是null

Since we know that the null pointer exception is coming from line 77: 由于我们知道空指针异常来自第77行:

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

We know that the string cannot be null since it is defined in quotes. 我们知道该字符串不能为null,因为它是用引号定义的。 the method setText can not be null since methods are not objects and can not be null!. setText方法不能为null,因为方法不是对象,也不能为null!。 That leaves us to the tvEmail object which must be null leading to a null pointer exception. 这使我们留给tvEmail对象,该对象必须为null导致空指针异常。 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为null,则您可能想重新计算对象或将其设置为类似于以下内容:

 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. 首先检查您在Activity或Fragment.etc。的onCreate方法中声明了您的对象。如果您想在调用onResume方法时检查该对象是否不为null,则它应该看起来像这样。 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. 记住在else中将为null,因为您说if不是,则这样做,否则它的null设置电子邮件不可用,但问题是在else变量中没有引用,换句话说,它为null,您还应该检查为什么此时将其null变量。

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. 这不是最好的选择,因为每次调用onResume都会在其上创建一个新对象。 You should check why the variable is null before onResume is called. 您应该在调用onResume之前检查为什么变量为null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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