I'm having a problem trying to read data from SQLite, I want to store all the information into an array of objects (nodes) that I already have defined.
My constructor and structure for that object is:
public node(int id, String name, String address, double x, double y, String type, String date){
//Initialization
//....
}
Note: The class of this object obviously already has defined the attributes, setter and getters. And the database has the same structure of that object (Int, String, String, Double, Double, String, String).
So, my method to get all the rows and store them into my array:
public nodo[] getNodes() {
String selectQuery = "SELECT * FROM " + NAME_NODES;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
node[] nodes = new node[cursor.getCount()];
int con = 0;
if (cursor.moveToFirst()) {
while (cursor.moveToNext()) {
node temp = new node(
cursor.getInt(1),
cursor.getString(2),
cursor.getString(3),
cursor.getDouble(4),
cursor.getDouble(5),
cursor.getString(6),
cursor.getString(7));
nodes[con++] = temp;
}
}
cursor.close();
db.close();
Log.d(TAG, "SQLite data: " + nodes.toString());
return nodes;
}
And then in other class I call this previous method, something like:
node[] nodes = db.getNodes();
String temp = node[0].getName();
But after that I get an error:
<code>E/CursorWindow: Failed to read row 1, column 7 from a CursorWindow which has 2 rows, 7 columns.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.motusk.AppMonitoreo, PID: 4410
java.lang.IllegalStateException: Couldn't read row 1, col 7 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetString(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:438)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
at com.motus.AppMonitoreo.Controladores.SQLiteHandler.getNodes(SQLiteHandler.java:186)</code>
I think the main problem is in the use of the cursor