2012-07-12 90 views
18

我得到errror:顯示java.lang.NullPointerException:需要的println消息時使用此梅索德:顯示java.lang.NullPointerException:需要的println消息

lst_info = new HashMap<String, String>(); 
SystemDatabaseHandler db = new SystemDatabaseHandler(getApplicationContext()); 
lst_info = db.getLstInfo(sql_id); 

SystemDatabaseHandler:

public HashMap<String , String> getLstInfo(int id){ 
    HashMap<String , String> lst_temp; 
    lst_temp = new HashMap<String , String>(); 

    String selectQuery = "SELECT * FROM dkr_lst_lst WHERE lst_id = " + id; 
    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor cursor = db.rawQuery(selectQuery, null); 
    if (cursor.moveToFirst()) { 
     do { 
      Log.d("sql_id", cursor.getString(0)); 
      lst_temp.put("sql_id", cursor.getString(0)); 
     }while (cursor.moveToNext()); 
    } 
    cursor.close(); 
    db.close(); 
    return lst_temp; 
} 

登錄.d(「sql_id」,cursor.getString(0));返回我:

11月7日至12日:56:05.603:d/SQL_ID(13783):1

日誌貓:

07-12 11:56:05.613: D/Database(13783): dbclose(): path = /data/data/de.bodprod.dkr/databases/dkr, handle = 0x4301a8 
07-12 11:56:05.613: D/AndroidRuntime(13783): Shutting down VM 
07-12 11:56:05.613: W/dalvikvm(13783): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0) 
07-12 11:56:05.623: E/AndroidRuntime(13783): FATAL EXCEPTION: main 
07-12 11:56:05.623: E/AndroidRuntime(13783): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.bodprod.dkr/de.bodprod.dkr.BosLstDetailMap}: java.lang.NullPointerException: println needs a message 
07-12 11:56:05.623: E/AndroidRuntime(13783): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872) 
07-12 11:56:05.623: E/AndroidRuntime(13783): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893) 
07-12 11:56:05.623: E/AndroidRuntime(13783): at android.app.ActivityThread.access$1500(ActivityThread.java:135) 
07-12 11:56:05.623: E/AndroidRuntime(13783): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054) 
07-12 11:56:05.623: E/AndroidRuntime(13783): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-12 11:56:05.623: E/AndroidRuntime(13783): at android.os.Looper.loop(Looper.java:150) 
07-12 11:56:05.623: E/AndroidRuntime(13783): at android.app.ActivityThread.main(ActivityThread.java:4385) 
07-12 11:56:05.623: E/AndroidRuntime(13783): at java.lang.reflect.Method.invokeNative(Native Method) 
07-12 11:56:05.623: E/AndroidRuntime(13783): at java.lang.reflect.Method.invoke(Method.java:507) 
07-12 11:56:05.623: E/AndroidRuntime(13783): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) 
07-12 11:56:05.623: E/AndroidRuntime(13783): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 
07-12 11:56:05.623: E/AndroidRuntime(13783): at dalvik.system.NativeStart.main(Native Method) 
07-12 11:56:05.623: E/AndroidRuntime(13783): Caused by: java.lang.NullPointerException: println needs a message 
07-12 11:56:05.623: E/AndroidRuntime(13783): at android.util.Log.println_native(Native Method) 
07-12 11:56:05.623: E/AndroidRuntime(13783): at android.util.Log.d(Log.java:137) 
07-12 11:56:05.623: E/AndroidRuntime(13783): at de.bodprod.dkr.BosLstDetailMap.onCreate(BosLstDetailMap.java:43) 
07-12 11:56:05.623: E/AndroidRuntime(13783): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072) 
07-12 11:56:05.623: E/AndroidRuntime(13783): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836) 
07-12 11:56:05.623: E/AndroidRuntime(13783): ... 11 more 
07-12 11:56:07.495: D/Process(13783): killProcess, pid=13783 

回答

30

也許當你做

Log.d("sql_id", cursor.getString(0)); 

cursor.getString(0)返回null,然後崩潰。

儘量做到:

if(cursor.getString(0) != null) { 
    Log.d("sql_id", cursor.getString(0)); 
} 

否則我們展示de.bodprod.dkr.BosLstDetailMap.onCreate(BosLstDetailMap.java:43)

+0

Log.d(「sql_id」,cursor.getString(0));返回1(參見上面的日誌文件) – Laire 2012-07-12 11:04:39

+1

發現錯誤,在主要活動中。我試圖抓住一個不存在的領域。你的提示幫助我找到了。 – Laire 2012-07-12 11:07:45

10

嘗試爲:

Log.d("sql_id"," " + cursor.getString(0)); 

而不是

Log.d("sql_id", cursor.getString(0)); 

怎麼一回事,因爲也許你逝去的第二個參數Log.d空或空

+0

相同的結果:07-12 13:01:26.271:d/SQL_ID(14234):1 – Laire 2012-07-12 11:02:40

+0

發現錯誤,是在主要活動。我試圖捕獲一個不存在的字段 – Laire 2012-07-12 11:06:53