Need help reading from SQLite database for Android

I read a lot of sites and manuals and could not solve my problem. I have an application that works until I try to read from my database (it opens it just fine). I know that this is not the right way to do something, but I have two global rows that I pass from my main activity to my DataBaseHelper class ( DB_TABLE and spinnerPipeLengthText ). When I call the next line of code to retrieve 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 enter it directly into the textual representations in the main.xml file. I do not know if this is possible, but it will be for me later. 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!

+4
source share
3 answers

Well, you close your cursor and then try to return it ... it won’t work so well. Either close the cursor and do not return anything, or return the cursor.

You also repeat the first first entries when you do movetofirst, movetonext, then movetofirst again.

I also believe that your getString needs a little help.

Here's how I say it:

 titleTxt.setText(eventedit.getString(eventedit .getColumnIndexOrThrow(AttendanceDB.EVENT_NAME))); 

titleTxt is the TextView link I installed earlier.
eventEdit is the cursor I want to get from (and created earlier)
AttendanceDB is my DB adapter class
EVENT_NAME is the column name variable that I set in the DB adapter class

EDIT

OK, now that I am re-reading your question, I understand that I am answering the wrong part. The problem is that your request is corrupted. I think it should be something like this:

 Cursor cursorData = myDataBase.query(CSSTPipeSizingActivity.DB_TABLE, null, COLUMN_ID + "=" + CSSTPipeSizingActivity.spinnerPipeLengthText, null, null, null, null, null); 

You need to indicate in which column the information you want is located (CSSTPipeSizingActivity.spinnerPipeLengthText). So replace COLUMN_ID with your column name or variable for it, and you should be good to go.

You really should get acquainted with the information here , especially with the various query method and their required parameters.

+1
source

From your logarithm, you can see that the error is a NullPointerException on line 173. Without line numbers in your code, it's hard to know exactly what the problem is in this case, but I assume line 173 is cursorData. moveToFirst (), which is not in the if statement.

Consider the case where cursorData is null -

You verify that cursorData is not null, in which case you call moveToFirst (). But then you call moveToFirst () on the next line; this will happen if cursorData is null or not.

A common design pattern would be for getData to simply return the cursor, and for the calling method to handle the extraction. It might look something like this:

{... if (cursorData! = null) cursorData.moveToFirst (); return cursorData; }

Hope this helps!

0
source

First, think that you can put all your code with the cursor in the IF statement:

  if (cursorData != null){ 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(); } 

Secondly, you can check the b / c code below to go to the following twice:

 cursorData.moveToNext(); CSSTPipeSizingActivity.textViewSize46.setText(cursorData.getString(1)); cursorData.moveToNext(); CSSTPipeSizingActivity.textViewSize62.setText(cursorData.getString(1)); 

Finally, use setText with string casting for the entire setText method:

 CSSTPipeSizingActivity.textViewSize15.setText("" + cursorData.getString(1)); 
0
source

Source: https://habr.com/ru/post/1401080/


All Articles