All i want to do is set a string equal to one of the entries in my database. The table i am accessing has 1 column (besides the ID column). I have been struggling with this for awhile so and even though similar questions have been asked, i am unable to make this situation work. I am comfortable with some other commands in SQLite but still a beginner. Here is what i have. I’m pretty sure once this method works my call from anther class will work fine.
note that both StringCategory_Table and COLUMN_CATEGORIES are defined as Strings.
my database was created with the following (it works fine for everything else except the below method):
db.execSQL("CREATE TABLE " + StringCategory_Table + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_CATEGORIES + " TEXT NOT NULL);");
and here is the method in question (created in a database class using SQLite open helper):
public String getCategory(int catId){
Cursor mycursor = myDatabase.rawQuery("SELECT " + COLUMN_CATEGORIES + " FROM "+ StringCategory_Table +" WHERE " + _ID + " = " + catId, null);
//i no longer use this line:String strCatName = mycursor.toString();
String strCatName = mycursor.getString(mycursor.getColumnIndex(COLUMN_CATEGORIES));
return strCatName;
}
here is my logcat for the above code:
09-04 20:05:47.764: E/AndroidRuntime(9232): FATAL EXCEPTION: main
09-04 20:05:47.764: E/AndroidRuntime(9232): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mypackage.namespace/com.mypackage.namespace.MyCurrentRestaurants}: java.lang.NullPointerException
09-04 20:05:47.764: E/AndroidRuntime(9232): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
09-04 20:05:47.764: E/AndroidRuntime(9232): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-04 20:05:47.764: E/AndroidRuntime(9232): at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-04 20:05:47.764: E/AndroidRuntime(9232): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-04 20:05:47.764: E/AndroidRuntime(9232): at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 20:05:47.764: E/AndroidRuntime(9232): at android.os.Looper.loop(Looper.java:137)
09-04 20:05:47.764: E/AndroidRuntime(9232): at android.app.ActivityThread.main(ActivityThread.java:4424)
09-04 20:05:47.764: E/AndroidRuntime(9232): at java.lang.reflect.Method.invokeNative(Native Method)
09-04 20:05:47.764: E/AndroidRuntime(9232): at java.lang.reflect.Method.invoke(Method.java:511)
09-04 20:05:47.764: E/AndroidRuntime(9232): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-04 20:05:47.764: E/AndroidRuntime(9232): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
Rather than Delete this question, I will explain what the problems were (as it will be annoying to dig through the comments):
- I can’t just assign a Class String to a SQLite entry as i was doing. Instead, i have to open the database before i can read from it: Since i need to open the database before i can read from it, it follows that i need to create an instance of the SQLite database class.
- Since i am using a cursor, i need to use the moveToFirst method before anything will work.
Thanks to everyone who has helped me today, and please let me know if these conclusions are incorrect as i don’t want to spread misconceptions to people as confused as i am.
Adam
Try this,