方法调用可能会产生java

方法调用可能会产生java

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


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


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

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()));

    }
}


这是我完整的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.access$800(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.access$800(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)

最佳答案

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


在这里,您只是初始化BOMResponse对象。您未在自定义对象内设置值,因此bomResponse.getObservations()将为null。您必须先设置值,然后再使用它。

09-29 19:43