i'm trying to query my database to check if a matching field value exists or not.
Pretty much using a SELECT sql statement.
I'm getting an NullPointerException error when i try to query the database even though the database is already open.
The following code is from the class holds all the SQLite methods:
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 Error
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
In one of my Activities i use an Object of the class to call the createDBCard() method.
Any help would be appreciated.
I tried playing around with the rawQuery() method but had errors, lol.
I fixed it, for now to where it returns my query result.
After debugging something else related to how i designed my open() method i found out, i guess from trying different things to solve some earlier errors, i declared the context as this instead of my pre-defined private final ourContext.
Below is the first debugged code that solved my later problem with my createDBCard() method, the NullPointerException.
My open() method after i declared my private final ourContext; before the DbHelper class with the rest of my other variables:
The one coded wrong below:
public CarddbAdapter open() throws SQLException{
ourHelper = new DbHelper(this);
ourDatabaseW = ourHelper.getWritableDatabase();
ourDatabaseR = ourHelper.getReadableDatabase();
return this;
}
The one coded right below:
public CarddbAdapter open() throws SQLException{
ourHelper = new DbHelper(ourContext);
ourDatabaseW = ourHelper.getWritableDatabase();
ourDatabaseR = ourHelper.getReadableDatabase();
return this;
}
As you can see i changed ourHelper = new DbHelper(this); to ourHelper = new DbHelper(ourContext);
Doing this made me realize i needed to change all the Toasts in my createDBCard() to use my pre-defined ourContext instead of this.
Thanks all for trying to help.