方法调用可能会产生 java.lang.nullpointerexception

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36159566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:00:59  来源:igfitidea点击:

method invocation may produce java.lang.nullpointerexception

javaandroidexceptionandroid-studionullpointerexception

提问by Mohamad Mustafa

When I try to run this, the emulator crashes and I get this Exception

当我尝试运行它时,模拟器崩溃并出现此异常

method invocation may produce java.lang.nullpointerexception

方法调用可能会产生 java.lang.nullpointerexception

Here is my code, I know the error is in the last line but I don't know how to fix it

这是我的代码,我知道错误在最后一行,但我不知道如何修复它

import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.net.MalformedURLException;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

        StrictMode.setThreadPolicy(policy);

        // FIXME: Experimenting

        try {

            ReadingFactory parser = new BOMFactory();
            WeatherService s = new WeatherService(
                    "BOM", "http://www.bom.gov.au/fwo/IDV60801/IDV60801.94839.json", parser
            );

            s.run();

         //   Thread test = new Thread(s);
           // test.start();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        BOMResponse bomResponse = new BOMResponse();
        TextView temperature  = (TextView)findViewById(R.id.temperature);
        temperature.setText(String.format("%d", bomResponse.getObservations().getData().get(0).getAirTemp().toString()));

    }
}

Here is my complete Stacktrace

这是我完整的 Stacktrace

FATAL EXCEPTION: main                                                                    
Process: com.example.teamredrmit.weatherapp, PID: 1792                                                                   
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.teamredrmit.weatherapp/com.example.teamredrmit.weathe
app.MainActivity}: java.lang.NullPointerException                                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)             at android.app.ActivityThread.access0(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at     com.example.teamredrmit.weatherapp.MainActivity.onCreate(MainActivity.java:41)
at android.app.Activity.performCreate(Activity.java:5231)
at     android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)?
at android.app.ActivityThread.access0(ActivityThread.java:135)?
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)?
at android.os.Handler.dispatchMessage(Handler.java:102)?
at android.os.Looper.loop(Looper.java:136)?
at android.app.ActivityThread.main(ActivityThread.java:5001)?
at java.lang.reflect.Method.invokeNative(Native Method)?
at java.lang.reflect.Method.invoke(Method.java:515)?
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)?
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)?
at dalvik.system.NativeStart.main(Native Method)

回答by KDeogharkar

 BOMResponse bomResponse = new BOMResponse();
        TextView temperature  = (TextView)findViewById(R.id.temperature);
        temperature.setText(String.format("%d", bomResponse.getObservations().getData().get(0).getAirTemp().toString()));

here you are just initializing your BOMResponseobject . you are not setting value inside your custom object so bomResponse.getObservations()will be null. you have to set value first then use it.

在这里,您只是在初始化您的BOMResponse对象。你没有设置你的自定义对象的内在价值,从而 bomResponse.getObservations()null。你必须先设置值然后使用它。