My code generates dynamic columns for some table based on some condition. The column may or may not exist.
So I am doing -
cur.getString(cur.getColumnIndex("Dynamic_Column_Name"))
The api for cur.getColumnIndex("Dynamic_Column_Name") says -
Returns the zero-based index for the given column name, or -1 if the column doesn't exist.
So if Dynamic_Column_Name
is not a column in my table then it will be--
cur.getString(-1)
The api for cur.getString(-1) says -
Returns the value of the requested column as a String. The result and whether this method throws an exception when the column value is null or the column type is not a string type is implementation-defined.
So what is the return type in my case or will it through some exception ?
Is there any best way to handle this situation ?
You will get an exception. Specifically:
java.lang.IllegalStateException: Couldn't read row n, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
To avoid it, check the column index is not -1 before using it.