我正在尝试查询我的数据库以检查是否存在匹配的字段值。
几乎使用 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() 方法,但有错误,哈哈。