使用SELECT语句查询数据库时SQLiteDatabase NullPointerException

时间:2013-05-11 01:28:40

标签: nullpointerexception android-cursor sqlite

我正在尝试查询我的数据库以检查是否存在匹配的字段值。

几乎使用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

在我的一个活动中,我使用类的Object来调用createDBCard()方法。

任何帮助都将不胜感激。

我尝试使用rawQuery()方法,但有错误,哈哈。

1 个答案:

答案 0 :(得分:0)

我修复了它,现在它将返回我的查询结果。

在调试了与我如何设计我的open()方法相关的其他内容之后我发现,我想通过尝试不同的东西来解决一些早期错误,我将上下文声明为 this 而不是我的前-defined private final ourContext

下面是第一个使用 createDBCard()方法解决我后来问题的调试代码,即NullPointerException。

我在DbHelper类之前使用其余的其他变量声明 private final ourContext; 之后的open()方法:

以下编码错误的一个:

public CarddbAdapter open() throws SQLException{
        ourHelper = new DbHelper(this);
        ourDatabaseW = ourHelper.getWritableDatabase();
        ourDatabaseR = ourHelper.getReadableDatabase();
        return this;
    }

下面编码的那个:

public CarddbAdapter open() throws SQLException{
        ourHelper = new DbHelper(ourContext);
        ourDatabaseW = ourHelper.getWritableDatabase();
        ourDatabaseR = ourHelper.getReadableDatabase();
        return this;
    }

如您所见,我将 ourHelper = new DbHelper(this); 更改为 ourHelper = new DbHelper(ourContext);

这样做让我意识到我需要更改 createDBCard()中的所有 Toast ,以使用我预先定义的 myContext 而不是的

感谢所有人的帮助。