pivotal/robolectric — Issue #195: ShadowSQLiteCursor.getString() throws ClassCastException for INTEGER type values
Author: sharafat
Created: 2012-02-14 11:27:31+00:00
State: open

I was writing a Database Exporter where I simply called cursor.getString() for column values of any type other than BLOB. It works fine, but when testing the code with robolectric, the following exception was thrown for an INTEGER type value:

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at com.xtremelabs.robolectric.shadows.ShadowSQLiteCursor.getString(ShadowSQLiteCursor.java:117)
.............

Peeking into the source of ShadowSQLiteCursor, I found out the implementation of the getString() method as follows:

public String getString(int columnIndex) {
checkPosition();
Object value = this.currentRow.get(getColumnNames()[columnIndex]);
if (value instanceof Clob) {
try {
return ((Clob) value).getSubString(1, (int)((Clob) value).length());
} catch (SQLException x) {
throw new RuntimeException(x);
}
} else {
return (String)value;
}
}

Note that in the last return statement, the value is casted to String which throws the ClassCastException for INTEGER type values. Now, in the source of Android 1.6, the implementation of the MatrixCursor.getString() method is as follows:

public String getString(int column) {
Object value = get(column);
if (value == null) return null;
return value.toString();
}

Here, the value.toString() is called which would work fine for both INTEGER and FLOAT type values.

Therefore, I think the ShadowSQLiteCursor.getString() should return value.toString() instead of (String) value.