0

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

几乎使用 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() 方法,但有错误,哈哈。

4

1 回答 1

0

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

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

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

在我声明了我的私有最终 ourContext 之后我的 open() 方法;在 DbHelper 类和我的其他变量之前:

下面的一个编码错误:

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); to ourHelper = new DbHelper(ourContext);

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

感谢大家尝试提供帮助。

于 2013-05-11T21:14:05.713 回答