我試圖查詢我的數據庫以檢查匹配字段值是否存在。SQLiteDatabase使用SELECT語句查詢數據庫時出現NullPointerException
非常多使用SELECT sql語句。
即使數據庫已經打開,當我試圖查詢數據庫時,出現NullPointerException錯誤。
下面的代碼是從類包含所有SQLite的方法:
public long createDBCard(String card_name, String card_type, String attributeType,
String summonRequirements, String card_description,
String spellSpeed, int levelStars, int rankStars, int atk_stat, int def_stat,
String setnumber, int cardNumberPass)
{
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
String passedCardName = card_name;
//long l = Long.parseLong(passedCardName);
String returnedCardName = getCardName(passedCardName);
long numCards = Countcards();
String numCardmsg = String.valueOf(numCards);
if (numCards == 51){
Toast h1 = Toast.makeText(CarddbAdapter.this, "Error counting card records.", Toast.LENGTH_LONG);
h1.show();
}else if(numCards == 0){
Toast h = Toast.makeText(this, "No cards exists", Toast.LENGTH_LONG);
h.show();
}else if(numCards >= 1){
Toast b = Toast.makeText(CarddbAdapter.this, returnedCardName, Toast.LENGTH_SHORT);
b.show();
}else{
Toast b = Toast.makeText(CarddbAdapter.this, "FAIL!!!!!!!!!!!!!!!!!", Toast.LENGTH_SHORT);
b.show();
}
cv.put(KEY_CARDNAME, card_name);
cv.put(KEY_CARDTYPE, card_type);
cv.put(KEY_ATTRIBUTETYPE, attributeType);
cv.put(KEY_SUMMONREQUIREMENTS, summonRequirements);
cv.put(KEY_CARD_DESCRIPTION, card_description);
cv.put(KEY_SPELLSPEED, spellSpeed);
cv.put(KEY_LEVELSTARS, levelStars);
cv.put(KEY_RANKSTARS, rankStars);
cv.put(KEY_ATK_STAT, atk_stat);
cv.put(KEY_DEF_STAT, def_stat);
cv.put(KEY_SETNUMBER, setnumber);
cv.put(KEY_CARDNUMBERPASS, cardNumberPass);
return ourDatabaseW.insert(DATABASE_TABLE_CARDS, null, cv);
//return 51;
}
public String getCardName(String passedName) throws SQLException{
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_CARDNAME};
Cursor c = ourDatabaseR.query(DATABASE_TABLE_CARDS, columns, KEY_CARDNAME + "='" + passedName + "'", null, null, null, null);
if (c != null){
c.moveToFirst();
String name = c.getString(0);
return name;
}
return "hello";
}
logcat的錯誤
05-11 01:15:49.456: E/AndroidRuntime(1509): FATAL EXCEPTION: main
05-11 01:15:49.456: E/AndroidRuntime(1509): java.lang.RuntimeException: Unable to start activity ComponentInfo{cybertech.productions.yugiohlibrary/cybertech.productions.yugiohlibrary.LoadingScreen}: java.lang.NullPointerException
05-11 01:15:49.456: E/AndroidRuntime(1509): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-11 01:15:49.456: E/AndroidRuntime(1509): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-11 01:15:49.456: E/AndroidRuntime(1509): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-11 01:15:49.456: E/AndroidRuntime(1509): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-11 01:15:49.456: E/AndroidRuntime(1509): at android.os.Handler.dispatchMessage(Handler.java:99)
05-11 01:15:49.456: E/AndroidRuntime(1509): at android.os.Looper.loop(Looper.java:123)
05-11 01:15:49.456: E/AndroidRuntime(1509): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-11 01:15:49.456: E/AndroidRuntime(1509): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 01:15:49.456: E/AndroidRuntime(1509): at java.lang.reflect.Method.invoke(Method.java:507)
05-11 01:15:49.456: E/AndroidRuntime(1509): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-11 01:15:49.456: E/AndroidRuntime(1509): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-11 01:15:49.456: E/AndroidRuntime(1509): at dalvik.system.NativeStart.main(Native Method)
05-11 01:15:49.456: E/AndroidRuntime(1509): Caused by: java.lang.NullPointerException
05-11 01:15:49.456: E/AndroidRuntime(1509): at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
05-11 01:15:49.456: E/AndroidRuntime(1509): at android.widget.Toast.<init>(Toast.java:89)
05-11 01:15:49.456: E/AndroidRuntime(1509): at android.widget.Toast.makeText(Toast.java:231)
05-11 01:15:49.456: E/AndroidRuntime(1509): at cybertech.productions.servicehelpers.CarddbAdapter.createDBCard(CarddbAdapter.java:144)
05-11 01:15:49.456: E/AndroidRuntime(1509): at cybertech.productions.yugiohlibrary.LoadingScreen.onCreate(LoadingScreen.java:37)
05-11 01:15:49.456: E/AndroidRuntime(1509): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-11 01:15:49.456: E/AndroidRuntime(1509): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-11 01:15:49.456: E/AndroidRuntime(1509): ... 11 more
在我的活動之一,我使用這個類的一個對象調用createDBCard( ) 方法。
任何幫助,將不勝感激。
我試着玩弄rawQuery()方法,但有錯誤,哈哈。
什麼行引發NullPointerException? – 2013-05-11 01:41:34
它並不完全告訴我在logcat中,但我知道它與getCardName()方法有關。 – 2013-05-11 01:50:00
你可以編輯問題來顯示logcat錯誤嗎? – chopchop 2013-05-11 02:25:15