【问题标题】:method invocation may produce java.lang.nullpointerexception方法调用可能产生 java.lang.nullpointerexception
【发布时间】:2016-03-22 16:10:29
【问题描述】:

当我尝试运行它时,模拟器崩溃并且我得到这个异常

方法调用可能产生 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)

【问题讨论】:

  • 添加完整的堆栈跟踪。
  • @Prera​​kSola 我添加了 Stacktrace
  • @Prera​​kSola 我想是这条线temperature.setText(String.format("%d", bomResponse.getObservations().getData().get(0).getAirTemp().toString()));

标签: java android exception android-studio nullpointerexception


【解决方案1】:
 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。你必须先设置值然后使用它。

【讨论】:

    猜你喜欢
    • 2016-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多