2012-07-08 52 views
-1
07-08 17:04:34.978: E/AndroidRuntime(322): FATAL EXCEPTION: main 
07-08 17:04:34.978: E/AndroidRuntime(322): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gurpswu.gurps/com.gurpswu.gurps.Home}: java.lang.NullPointerException 
07-08 17:04:34.978: E/AndroidRuntime(322): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
07-08 17:04:34.978: E/AndroidRuntime(322): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
07-08 17:04:34.978: E/AndroidRuntime(322): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
07-08 17:04:34.978: E/AndroidRuntime(322): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
07-08 17:04:34.978: E/AndroidRuntime(322): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-08 17:04:34.978: E/AndroidRuntime(322): at android.os.Looper.loop(Looper.java:123) 
07-08 17:04:34.978: E/AndroidRuntime(322): at android.app.ActivityThread.main(ActivityThread.java:3683) 
07-08 17:04:34.978: E/AndroidRuntime(322): at java.lang.reflect.Method.invokeNative(Native Method) 
07-08 17:04:34.978: E/AndroidRuntime(322): at java.lang.reflect.Method.invoke(Method.java:507) 
07-08 17:04:34.978: E/AndroidRuntime(322): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
07-08 17:04:34.978: E/AndroidRuntime(322): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
07-08 17:04:34.978: E/AndroidRuntime(322): at dalvik.system.NativeStart.main(Native Method) 
07-08 17:04:34.978: E/AndroidRuntime(322): Caused by: java.lang.NullPointerException 
07-08 17:04:34.978: E/AndroidRuntime(322): at com.gurpswu.gurps.Player.getIntField(Player.java:153) 
07-08 17:04:34.978: E/AndroidRuntime(322): at com.gurpswu.gurps.Home.hudSetup(Home.java:102) 
07-08 17:04:34.978: E/AndroidRuntime(322): at com.gurpswu.gurps.Home.onCreate(Home.java:25) 
07-08 17:04:34.978: E/AndroidRuntime(322): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
07-08 17:04:34.978: E/AndroidRuntime(322): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
07-08 17:04:34.978: E/AndroidRuntime(322): ... 11 more 

我有一個遊戲。當用戶第一次打開應用程序並按下繼續遊戲按鈕時,遊戲將嘗試使用下面的代碼從數據庫中檢索值。NullPointer由於數據庫不包含數據

  stats.open(); 
    playerId = stats.getIntField(stats.KEY_ROWID); 
    String playerName = stats.getStringField(stats.KEY_NAME); 
    String playerCity = stats.getStringField(stats.KEY_CITY); 
    playerHealth = stats.getIntField(stats.KEY_HEALTH); 
    playerEnergy = stats.getIntField(stats.KEY_ENERGY); 
    playerRank = stats.getIntField(stats.KEY_RANK); 
    playerCash = stats.getIntField(stats.KEY_CASH); 
    stats.close(); 

然而,這些數據庫字段都填充...如何安裝check..so,如果數據庫存在此代碼只運行。如果它不存在,那麼它會要求玩家開始一個新的遊戲。

+0

這不是最乾淨的解決方案,但你可以使用try {} ..趕上{} – 2012-07-08 17:18:58

回答

1

在做stats.open()之後必須檢查stats,你應該調用返回行數的東西。你可以把一個條件,如果行是0

,或者你可以嘗試捕捉與NullPExe

try{ 
    stats.open(); 
    playerId = stats.getIntField(stats.KEY_ROWID); 
    String playerName = stats.getStringField(stats.KEY_NAME); 
    String playerCity = stats.getStringField(stats.KEY_CITY); 
    playerHealth = stats.getIntField(stats.KEY_HEALTH); 
    playerEnergy = stats.getIntField(stats.KEY_ENERGY); 
    playerRank = stats.getIntField(stats.KEY_RANK); 
    playerCash = stats.getIntField(stats.KEY_CASH); 
    stats.close(); 
} catch(NullPointerException e) { .. do needful or return null }