Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1. Error - Android

I'm working on Android application and when trying to get SQLite database data as TextView (only one record in the table) facing this error.

The log and exception:

03-28 01:37:22.137: E/AndroidRuntime(9585): FATAL EXCEPTION: main
03-28 01:37:22.137: E/AndroidRuntime(9585): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tourinfo/com.example.tourinfo.TourInfo}: android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1
03-28 01:37:22.137: E/AndroidRuntime(9585):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at android.os.Looper.loop(Looper.java:123)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at android.app.ActivityThread.main(ActivityThread.java:3683)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at java.lang.reflect.Method.invokeNative(Native Method)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at java.lang.reflect.Method.invoke(Method.java:507)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at dalvik.system.NativeStart.main(Native Method)
03-28 01:37:22.137: E/AndroidRuntime(9585): Caused by: android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1
03-28 01:37:22.137: E/AndroidRuntime(9585):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at com.example.tourinfo.TourInfo.onCreate(TourInfo.java:44)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-28 01:37:22.137: E/AndroidRuntime(9585):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-28 01:37:22.137: E/AndroidRuntime(9585):     ... 11 more

This is the java code:

public class TourInfo extends Activity{


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tour_info);

    TourInfoDatabaseHelper jobInfoDatabaseHelper = new TourInfoDatabaseHelper(this);



    Cursor cursorJObInfo = jobInfoDatabaseHelper.getAllJobInfoRecords();
    if(cursorJObInfo.moveToFirst()){
        do{
            String startLocation = cursorJObInfo.getString(1);
            String endLocation = cursorJObInfo.getString(2);
            String type = cursorJObInfo.getString(3);
            Log.d("DB value", startLocation +" "+ endLocation +" "+ type);
        }while(cursorJObInfo.moveToNext());
    }


//  if(!cursorJObInfo.isClosed()){
//      cursorJObInfo.close();
 //   }
       TextView startLocationView = (TextView) findViewById(R.id.start_location_view);
        startLocationView.setText(cursorJObInfo.getString(1));
        TextView endLocationView = (TextView) findViewById(R.id.end_location_view);
        endLocationView.setText(cursorJObInfo.getString(2));
        TextView typeOfJourneyView = (TextView) findViewById(R.id.type_of_journey_view);
        typeOfJourneyView.setText(cursorJObInfo.getString(3));


}


public void onClickedLocations(View view){

    Intent intent = new Intent(this, LocationList.class);
    startActivity(intent);

}

}

TourInfoDatabaseHelper.java class

  public class TourInfoDatabaseHelper {

 // private static final int DATABASE_VERSION= 2;
 // private static final String DATABASE_NAME = "tourinfo.db";
private static final String TABLE_NAME = "tour_info_details";

public static final String JOBINFO_COLUMN_JOB_ID = "job_id";
public static final String JOBINFO_COLUMN_START_LOCATION = "start_location";
public static final String JOBINFO_COLUMN_END_LOCATION = "end_location";
public static final String JOBINFO_COLUMN_TYPE = "type";
public static final String JOBINFO_COLUMN_TOUR_NUMBER = "tour_number";
public static final String JOBINFO_COLUMN_USER_ID ="user_id";


private SQLiteDatabase jobInfoDatabase;
private JobInfoOpenHelper jobInfoOpenHelper;

public TourInfoDatabaseHelper (Context context) {
    jobInfoOpenHelper = new JobInfoOpenHelper(context);
    jobInfoDatabase = jobInfoOpenHelper.getWritableDatabase();
} 

public void saveJobInfoRecords (String startLocation,String endLocation,String type,String tourNumber,int userId){

    ContentValues contentValues = new ContentValues();

//  contentValues.put(JOBINFO_COLUMN_JOB_ID, jobId);
    contentValues.put(JOBINFO_COLUMN_START_LOCATION, startLocation);
    contentValues.put(JOBINFO_COLUMN_END_LOCATION, endLocation);
    contentValues.put(JOBINFO_COLUMN_TYPE, type);
    contentValues.put(JOBINFO_COLUMN_TOUR_NUMBER, tourNumber);
    contentValues.put(JOBINFO_COLUMN_USER_ID, userId);

    jobInfoDatabase.insert(TABLE_NAME, null, contentValues);

}

public Cursor getAllJobInfoRecords(){
    return jobInfoDatabase.rawQuery(" SELECT * FROM " + TABLE_NAME, null);
}

private class JobInfoOpenHelper extends SQLiteOpenHelper {


    JobInfoOpenHelper(Context context) {
        super(context,MainDBAdapter.DATABASE_NAME,null,MainDBAdapter.DATABASE_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase jobInfoDatabase) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase jobInfoDatabase, int oldVersion, int newVersion) {
    //  jobInfoDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    //  onCreate(jobInfoDatabase);

    }

}


}

Please help me for avoid this error.

like image 495
user2881604 Avatar asked Mar 27 '14 08:03

user2881604


1 Answers

You loop through your cursor in the do-while loop but access cursor data with getString() after it, when the cursor is pointing after the last result row.

It's a little unclear what you want to do: You're fetching all records but are only using one result row. As a quick fix, capture the values inside the do-while and not after, e.g.

String startLocation = null;
String endLocation = null;
String type = null;

if(cursorJObInfo.moveToFirst()){
    do{
        startLocation = cursorJObInfo.getString(1);
        endLocation = cursorJObInfo.getString(2);
        type = cursorJObInfo.getString(3);
        Log.d("DB value", startLocation +" "+ endLocation +" "+ type);
    }while(cursorJObInfo.moveToNext());
}

// ...
startLocationView.setText(startLocation); // instead of calling getString() on the cursor here
// the same for the other textviews
like image 165
laalto Avatar answered Sep 30 '22 14:09

laalto