Search code examples
androidsqlitecursorindexoutofboundsexception

Android retrieve table data CursorIndexOutOfBoundsException


I need to display all table data into list view. I have getting error while trying to view the items in list view. There are two items in first list view. When clicking the item need to display all items containing with that item in next list view. Data is inserting in database but not displaying in list view. My log cat is also given below..

public class EmployeeDAO {

public static final String TAG = "EmployeeDAO";

private Context mContext;

// Database fields
private SQLiteDatabase mDatabase;
private DBHelper mDbHelper;
private String[] mAllColumns = { DBHelper.COLUMN_LATLONG_ID, DBHelper.COLUMN_LOCATION_NAME,
        DBHelper.COLUMN_LOCATION_ADDRESS,DBHelper.COLUMN_LATITUDE,DBHelper.COLUMN_LONGITUDE};

public EmployeeDAO(Context context) {
    mDbHelper = new DBHelper(context);
    this.mContext = context;
    // open the database
    try {
        open();
    }
    catch(SQLException e) {
        Log.e(TAG, "SQLException on openning database " + e.getMessage());
        e.printStackTrace();
    }
}

public void open() throws SQLException {
    mDatabase = mDbHelper.getWritableDatabase();
}

public void close() {
    mDbHelper.close();
}

public Employee createEmploye(String place, String location, String latitude, String longitude, long companyId) {
    ContentValues values = new ContentValues();
    values.put(DBHelper.COLUMN_LOCATION_NAME, place);
    values.put(DBHelper.COLUMN_LOCATION_ADDRESS, location);
    values.put(DBHelper.COLUMN_LATITUDE, latitude);
    values.put(DBHelper.COLUMN_LONGITUDE, longitude);
    values.put(DBHelper.COLUMN_LATLONG_LOC_ID, companyId);
    long insertId = mDatabase.insert(DBHelper.TABLE_LATLONG, null, values);
    Cursor cursor = mDatabase.query(DBHelper.TABLE_LATLONG,
            mAllColumns, DBHelper.COLUMN_LATLONG_ID + " = " + insertId, null, null, null, null);
    cursor.moveToFirst();
    Employee newEmployee = cursorToEmploye(cursor);
    cursor.close();
    return newEmployee;
}

public void deleteEmployee(Employee employee) {
    long id = employee.getId();
    System.out.println("the deleted employee has the id: " + id);
    mDatabase.delete(DBHelper.TABLE_LATLONG, DBHelper.COLUMN_LATLONG_ID + " = " + id, null);
}

public List<Employee> getAllEmployees() {
    List<Employee> listEmployees = new ArrayList<Employee>();

    Cursor cursor = mDatabase.query(DBHelper.TABLE_LATLONG,
            mAllColumns, null, null, null, null, null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        Employee employee = cursorToEmploye(cursor);
        listEmployees.add(employee);
        cursor.moveToNext();
    }
    // make sure to close the cursor
    cursor.close();
    return listEmployees;
}

public List<Employee> getEmployeesOfCompany(long companyId) {         <-- getting error code here
    List<Employee> listEmployees = new ArrayList<Employee>();

    Cursor cursor = mDatabase.query(DBHelper.TABLE_LATLONG, mAllColumns
            , DBHelper.COLUMN_LATLONG_LOC_ID + " = ?",
            new String[] { String.valueOf(companyId) }, null, null, null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        Employee employee = cursorToEmploye(cursor);
        listEmployees.add(employee);
        cursor.moveToNext();
    }

    cursor.close();
    return listEmployees;
}

private Employee cursorToEmploye(Cursor cursor) {   <--- getting error code here
    Employee employee = new Employee();
    employee.setId(cursor.getLong(0));
    employee.setFirstName(cursor.getString(1));
    employee.setLastName(cursor.getString(2));


    long companyId = cursor.getLong(3);
    CompanyDAO dao = new CompanyDAO(mContext);
    Locations company = dao.getCompanyById(companyId);
    if(company != null)
        employee.setCompany(company);

    return employee;
}

}

Logcat

06-10 01:34:15.175: E/AndroidRuntime(16289): FATAL EXCEPTION: main
06-10 01:34:15.175: E/AndroidRuntime(16289): Process: com.example.locationstable, PID: 16289
06-10 01:34:15.175: E/AndroidRuntime(16289): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.locationstable/com.example.locationstable.ListEmployeesActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
06-10 01:34:15.175: E/AndroidRuntime(16289):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at android.os.Looper.loop(Looper.java:136)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at android.app.ActivityThread.main(ActivityThread.java:5017)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at java.lang.reflect.Method.invokeNative(Native Method)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at java.lang.reflect.Method.invoke(Method.java:515)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at dalvik.system.NativeStart.main(Native Method)
06-10 01:34:15.175: E/AndroidRuntime(16289): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
06-10 01:34:15.175: E/AndroidRuntime(16289):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:74)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at com.example.locationstable.CompanyDAO.cursorToLocation(CompanyDAO.java:111)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at com.example.locationstable.CompanyDAO.getCompanyById(CompanyDAO.java:105)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at com.example.locationstable.EmployeeDAO.cursorToEmploye(EmployeeDAO.java:113)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at com.example.locationstable.EmployeeDAO.getEmployeesOfCompany(EmployeeDAO.java:94)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at com.example.locationstable.ListEmployeesActivity.onCreate(ListEmployeesActivity.java:85)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at android.app.Activity.performCreate(Activity.java:5231)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-10 01:34:15.175: E/AndroidRuntime(16289):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-10 01:34:15.175: E/AndroidRuntime(16289):    ... 11 more

Solution

  • You're not closing the cursor in one of your methods

    public Locations getCompanyById(long id) {
    Cursor cursor = mDatabase.query(SQLiteDatabaseData.TABLE_LOC, mAllColumns,
            SQLiteDatabaseData.COLUMN_ID + " = ?",
            new String[] { String.valueOf(id) }, null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
    }
    
    Locations locations = cursorToLocation(cursor);
    // Close the cursor
    cursor.close();
    return locations;
    }