0

In my application, there is a few lines of code for retrieving data from multiple database.

Code:

String testquiry = "select a.rowid as _id, "
                    + "a.delivery_seq sequence, "
                    + "a.chinese_name chinese_name, "
                    + "b.cust_code cust_code, "             
                    + "a.status status "
                    + "from route_cust a, customer_file b, pda c "
                    + "where a.cust_code = b.cust_code "
                    + "and b.cust_status = 'A' "
                    + "and b.route = c.route "              
                    + "and c.default_route = 'Y' "
                    + "order by a.delivery_seq";


String testResult;

Cursor testCur = db.rawQuery(testquiry, null);
    if(testCur.moveToFirst()){
        testResult = testCur.getString(cur_getimportcsv.getColumnIndex("status"));
        Toast.makeText(v.getContext(), testResult, Toast.LENGTH_LONG).show();
    }

testCur.close();

When the application run to the following code, errors occur.

testResult = testCur.getString(cur_getimportcsv.getColumnIndex("status"));

Error displayed in LogCat:

09-03 15:53:36.533: W/dalvikvm(23398): threadid=1: thread exiting with uncaught exception (group=0x415abba8)
09-03 15:53:36.563: E/AndroidRuntime(23398): FATAL EXCEPTION: main
09-03 15:53:36.563: E/AndroidRuntime(23398): Process: com.iceapp, PID: 23398
09-03 15:53:36.563: E/AndroidRuntime(23398): java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
09-03 15:53:36.563: E/AndroidRuntime(23398):    at android.database.CursorWindow.nativeGetString(Native Method)
09-03 15:53:36.563: E/AndroidRuntime(23398):    at android.database.CursorWindow.getString(CursorWindow.java:434)
09-03 15:53:36.563: E/AndroidRuntime(23398):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
09-03 15:53:36.563: E/AndroidRuntime(23398):    at com.iceapp.ImportCsvActivity$2.onClick(ImportCsvActivity.java:285)
09-03 15:53:36.563: E/AndroidRuntime(23398):    at android.view.View.performClick(View.java:4438)
09-03 15:53:36.563: E/AndroidRuntime(23398):    at android.view.View$PerformClick.run(View.java:18422)
09-03 15:53:36.563: E/AndroidRuntime(23398):    at android.os.Handler.handleCallback(Handler.java:733)
09-03 15:53:36.563: E/AndroidRuntime(23398):    at android.os.Handler.dispatchMessage(Handler.java:95)
09-03 15:53:36.563: E/AndroidRuntime(23398):    at android.os.Looper.loop(Looper.java:136)
09-03 15:53:36.563: E/AndroidRuntime(23398):    at android.app.ActivityThread.main(ActivityThread.java:5001)
09-03 15:53:36.563: E/AndroidRuntime(23398):    at java.lang.reflect.Method.invokeNative(Native Method)
09-03 15:53:36.563: E/AndroidRuntime(23398):    at java.lang.reflect.Method.invoke(Method.java:515)
09-03 15:53:36.563: E/AndroidRuntime(23398):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
09-03 15:53:36.563: E/AndroidRuntime(23398):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
09-03 15:53:36.563: E/AndroidRuntime(23398):    at dalvik.system.NativeStart.main(Native Method)

I have studied other similar question on stackoverflow, I found that this problem was caused by unsuccessful fetch of column "status". Can anyone give me some help? Why the value of 'status' cannot be got successfully??

1
  • 1
    you should get column index from testCur not from cur_getimportcsv Commented Sep 3, 2014 at 9:14

2 Answers 2

1

You're using different cursor (cur_getimportcsv) as the reference for finding column "status". It might be exist, or it might be not. In this case, it is not: there is no "status" on cur_getimportcsv, returning "-1" for the position.

It's almost never safe to use different cursor when manipulating a cursor. The solution is to use same cursor all the way.

testResult = testCur.getString(testCur.getColumnIndex("status"));
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I did't see this careless mistake.
0

If you want to read only the first row, you can do

testCur.moveToFirst();
if (!testCur.isAfterLast()) {
    testResult = testCur.getString(testCur.getColumnIndex("status"));
}

Edit:

You can iterate through your cursor as follows

testCur.moveToFirst();
while (testCur.isAfterLast() == false) 
{
    testResult = testCur.getString(testCur.getColumnIndex("status"));

    // implement your logic here       

    cur.moveToNext();
}

3 Comments

@Selvin Yes, I want to read the first row only, but I will try bhargavg's suggestion first
Nope, was editing my answer before I saw your comment
ok, ok anyway if(testCur.moveToFirst()){} is way better then testCur.moveToFirst(); if (!testCur.isAfterLast()){}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.