应用程序抛出NullPointerException,但无法找到原因

时间:2015-01-14 18:44:21

标签: java android

据我所知,我使用调试器检查了所有变量,并且它们都不为空。

来自GameService.java的我的“getCurrentGameId”方法:

public long getCurrentGameId() {
    if(mGame != null && mGame.mModel != null) {
        return mGame.mModel.getId();
    }
    return 0;
}

错误:

Process: com.soccertimer.android.debug, PID: 28143
java.lang.NullPointerException
        at com.soccertimer.android.services.GameService.getCurrentGameId(GameService.java:107)
        at com.soccertimer.android.ui.GameListFragment.onListItemClick(GameListFragment.java:82)
        at com.soccertimer.android.ui.GameListFragment$$ViewInjector$1.onItemClick(GameListFragment$$ViewInjector.java:20)
        at android.widget.AdapterView.performItemClick(AdapterView.java:298)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:2911)
        at android.widget.AbsListView$3.run(AbsListView.java:3645)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5146)
        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:796)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

由于返回getCurrentGameId()的{​​{1}}方法与返回long的{​​{1}}方法之间发生冲突而引发异常。 因此,当getId()方法返回Long时,会抛出getId()

更改null)以返回NullPointerException就可以了。