I have read through many sites and tutorials and have been unable to solve my problem. I have an application that works until I try to read from my database (it opens it just fine). I know its not the right way to do things but I have two global strings that I pass from my main activity to my DataBaseHelper class (DB_TABLE
and spinnerPipeLengthText
). When I call the following line of code to retrieve the data from all the columns in a specific row my application crashes.
Cursor cursorData = myDataBase.query(CSSTPipeSizingActivity.DB_TABLE, null,
CSSTPipeSizingActivity.spinnerPipeLengthText, null, null, null, null, null);
Here is my complete getData
method:
public Cursor getData() {
Cursor cursorData = myDataBase.query(CSSTPipeSizingActivity.DB_TABLE, null,
CSSTPipeSizingActivity.spinnerPipeLengthText, null, null, null, null, null);
if (cursorData != null)
cursorData.moveToFirst();
cursorData.moveToFirst();
CSSTPipeSizingActivity.textViewSize15.setText(cursorData.getString(1));
cursorData.moveToNext();
CSSTPipeSizingActivity.textViewSize19.setText(cursorData.getString(1));
cursorData.moveToFirst();
CSSTPipeSizingActivity.textViewSize25.setText(cursorData.getString(1));
cursorData.moveToNext();
CSSTPipeSizingActivity.textViewSize31.setText(cursorData.getString(1));
cursorData.moveToFirst();
CSSTPipeSizingActivity.textViewSize37.setText(cursorData.getString(1));
cursorData.moveToNext();
CSSTPipeSizingActivity.textViewSize46.setText(cursorData.getString(1));
cursorData.moveToNext();
CSSTPipeSizingActivity.textViewSize62.setText(cursorData.getString(1));
cursorData.close();
return cursorData;
} // end method getData
As you can see from my code, I am trying to take the data and input it straight into text views in the main.xml
file. I don't know if this is possible but that will be for me to figure out later. Right now I need to get the data.
Here is my logcat:
03-12 19:23:33.587: D/AndroidRuntime(20191): Shutting down VM
03-12 19:23:33.587: W/dalvikvm(20191): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-12 19:23:33.617: E/AndroidRuntime(20191): FATAL EXCEPTION: main
03-12 19:23:33.617: E/AndroidRuntime(20191): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.skyesmechanical.CSSTPipeSizing/com.skyesmechanical.CSSTPipeSizing.CSSTPipeSizingActivity}: java.lang.NullPointerException
03-12 19:23:33.617: E/AndroidRuntime(20191): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-12 19:23:33.617: E/AndroidRuntime(20191): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-12 19:23:33.617: E/AndroidRuntime(20191): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-12 19:23:33.617: E/AndroidRuntime(20191): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-12 19:23:33.617: E/AndroidRuntime(20191): at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 19:23:33.617: E/AndroidRuntime(20191): at android.os.Looper.loop(Looper.java:123)
03-12 19:23:33.617: E/AndroidRuntime(20191): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-12 19:23:33.617: E/AndroidRuntime(20191): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 19:23:33.617: E/AndroidRuntime(20191): at java.lang.reflect.Method.invoke(Method.java:507)
03-12 19:23:33.617: E/AndroidRuntime(20191): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-12 19:23:33.617: E/AndroidRuntime(20191): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-12 19:23:33.617: E/AndroidRuntime(20191): at dalvik.system.NativeStart.main(Native Method)
03-12 19:23:33.617: E/AndroidRuntime(20191): Caused by: java.lang.NullPointerException
03-12 19:23:33.617: E/AndroidRuntime(20191): at com.skyesmechanical.CSSTPipeSizing.DataBaseHelper.getData(DataBaseHelper.java:173)
03-12 19:23:33.617: E/AndroidRuntime(20191): at com.skyesmechanical.CSSTPipeSizing.CSSTPipeSizingActivity.onCreate(CSSTPipeSizingActivity.java:105)
03-12 19:23:33.617: E/AndroidRuntime(20191): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-12 19:23:33.617: E/AndroidRuntime(20191): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-12 19:23:33.617: E/AndroidRuntime(20191): ... 11 more
Thanks for your help!
Well, you' re closing your cursor and then trying to return it... not gonna work too well that way. Either close the cursor and return nothing or return the cursor.
You also keep iterating over the first couple entries when you do movetofirst, movetonext, then movetofirst again.
I also believe your getString needs a little help.
Here's how I go about it:
titleTxt.setText(eventedit.getString(eventedit
.getColumnIndexOrThrow(AttendanceDB.EVENT_NAME)));
titleTxt
is a TextView reference I set earlier.
eventEdit
is the cursor I'm wanting to get the info from (and created earlier)
AttendanceDB
is my DB adapter class
EVENT_NAME
is the column name variable I set in my DB adapter class
EDIT
Ok, now that I re-read your question, I realize I was answering the wrong portion. The problem is your query is messed up. I think it should be more like this:
Cursor cursorData = myDataBase.query(CSSTPipeSizingActivity.DB_TABLE, null,
COLUMN_ID + "=" + CSSTPipeSizingActivity.spinnerPipeLengthText, null, null, null, null, null);
You need to tell the query what column to search for the info you want (CSSTPipeSizingActivity.spinnerPipeLengthText). So replace COLUMN_ID with your column name or the variable for it and you should be good to go.
You really should review the info here, especially the various query method and their required parameters.