Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When I add a new field I get a null pointer

I have my ORMlite service working fine. All CRUD operations work fine. However when I add a new nullable field to my entity, the whole thing fails. I get null pointers and the application dies.

This is the field I added:

@DatabaseField( canBeNull = true  )
private String newField;

If I drop my DB and recreate it then everything is fine. What I would like to know is why does a simple schema change cause this? I'm thinking ahead where a DB is full with data and I don't want a new nullable field to break it or is this to be expected?

Here's the exception:

W/System.err(16955): java.sql.SQLException: Unknown field 'newField' from the Android sqlite cursor, not in:[desc, name, id]
W/System.err(16955):    at com.j256.ormlite.android.AndroidDatabaseResults.findColumn(AndroidDatabaseResults.java:97)
W/System.err(16955):    at com.j256.ormlite.field.FieldType.resultToJava(FieldType.java:793)
W/System.err(16955):    at com.j256.ormlite.stmt.mapped.BaseMappedQuery.mapRow(BaseMappedQuery.java:60)
W/System.err(16955):    at com.j256.ormlite.stmt.SelectIterator.getCurrent(SelectIterator.java:270)

Here is the exception I'm getting when I update my entity class to include the following new field.

@DatabaseField( canBeNull = true  )
private String newField;

Now seeing that if I remove the new field my test case/app works fine then it is this code that breaks things .

[2012-07-05 22:42:00 - WishListManager] Success!
[2012-07-05 22:42:00 - WishListManagerTest] Launching instrumentation android.test.InstrumentationTestRunner on device emulator-5554
[2012-07-05 22:42:00 - WishListManagerTest] Collecting test information
[2012-07-05 22:42:02 - WishListManagerTest] Sending test information to Eclipse
[2012-07-05 22:42:02 - WishListManagerTest] Running tests...
[2012-07-05 22:42:04 - WishListManagerTest] Test run failed: Instrumentation run failed due to 'java.lang.NullPointerException'
[2012-07-05 22:42:04 - WishListManagerTest] Test run finished


07-05 21:42:02.084: I/TestRunner(16955): started: testAddDuplicateWishLists(com.tests.WishListServiceTest)
07-05 21:42:02.504: I/global(16955): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
07-05 21:42:02.524: I/global(16955): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
07-05 21:42:02.654: D/dalvikvm(16955): GC_FOR_MALLOC freed 4623 objects / 361912 bytes in 63ms
07-05 21:42:02.754: I/global(16955): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
07-05 21:42:02.764: I/global(16955): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
07-05 21:42:02.774: W/System.err(16955): java.sql.SQLException: Unknown field 'newField' from the Android sqlite cursor, not in:[desc, name, id]
07-05 21:42:02.784: W/System.err(16955):    at com.j256.ormlite.android.AndroidDatabaseResults.findColumn(AndroidDatabaseResults.java:97)
07-05 21:42:02.784: W/System.err(16955):    at com.j256.ormlite.field.FieldType.resultToJava(FieldType.java:793)
07-05 21:42:02.784: W/System.err(16955):    at com.j256.ormlite.stmt.mapped.BaseMappedQuery.mapRow(BaseMappedQuery.java:60)
07-05 21:42:02.784: W/System.err(16955):    at com.j256.ormlite.stmt.SelectIterator.getCurrent(SelectIterator.java:270)
07-05 21:42:02.784: W/System.err(16955):    at com.j256.ormlite.stmt.SelectIterator.nextThrow(SelectIterator.java:161)
07-05 21:42:02.784: W/System.err(16955):    at com.j256.ormlite.stmt.StatementExecutor.query(StatementExecutor.java:161)
07-05 21:42:02.784: W/System.err(16955):    at com.j256.ormlite.stmt.StatementExecutor.queryForAll(StatementExecutor.java:113)
07-05 21:42:02.784: W/System.err(16955):    at com.j256.ormlite.dao.BaseDaoImpl.queryForAll(BaseDaoImpl.java:237)
07-05 21:42:02.784: W/System.err(16955):    at com.test.db.WishListService.getAllWishLists(WishListService.java:42)
07-05 21:42:02.784: W/System.err(16955):    at com.test.WishListManagerActivity.setupListView(WishListManagerActivity.java:48)
07-05 21:42:02.784: W/System.err(16955):    at com.test.WishListManagerActivity.onStart(WishListManagerActivity.java:44)
07-05 21:42:02.784: W/System.err(16955):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
07-05 21:42:02.784: W/System.err(16955):    at android.app.Activity.performStart(Activity.java:3781)
07-05 21:42:02.784: W/System.err(16955):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
07-05 21:42:02.784: W/System.err(16955):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-05 21:42:02.794: W/System.err(16955):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-05 21:42:02.794: W/System.err(16955):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-05 21:42:02.794: W/System.err(16955):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 21:42:02.794: W/System.err(16955):    at android.os.Looper.loop(Looper.java:123)
07-05 21:42:02.794: W/System.err(16955):    at android.app.ActivityThread.main(ActivityThread.java:4627)
07-05 21:42:02.794: W/System.err(16955):    at java.lang.reflect.Method.invokeNative(Native Method)
07-05 21:42:02.794: W/System.err(16955):    at java.lang.reflect.Method.invoke(Method.java:521)
07-05 21:42:02.794: W/System.err(16955):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-05 21:42:02.794: W/System.err(16955):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-05 21:42:02.794: W/System.err(16955):    at dalvik.system.NativeStart.main(Native Method)
07-05 21:42:02.804: D/AndroidRuntime(16955): Shutting down VM
07-05 21:42:02.804: W/dalvikvm(16955): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-05 21:42:02.814: E/AndroidRuntime(16955): FATAL EXCEPTION: main
07-05 21:42:02.814: E/AndroidRuntime(16955): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.test.WishListManagerActivity}: java.lang.NullPointerException
07-05 21:42:02.814: E/AndroidRuntime(16955):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-05 21:42:02.814: E/AndroidRuntime(16955):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-05 21:42:02.814: E/AndroidRuntime(16955):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-05 21:42:02.814: E/AndroidRuntime(16955):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-05 21:42:02.814: E/AndroidRuntime(16955):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 21:42:02.814: E/AndroidRuntime(16955):    at android.os.Looper.loop(Looper.java:123)
07-05 21:42:02.814: E/AndroidRuntime(16955):    at android.app.ActivityThread.main(ActivityThread.java:4627)
07-05 21:42:02.814: E/AndroidRuntime(16955):    at java.lang.reflect.Method.invokeNative(Native Method)
07-05 21:42:02.814: E/AndroidRuntime(16955):    at java.lang.reflect.Method.invoke(Method.java:521)
07-05 21:42:02.814: E/AndroidRuntime(16955):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-05 21:42:02.814: E/AndroidRuntime(16955):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-05 21:42:02.814: E/AndroidRuntime(16955):    at dalvik.system.NativeStart.main(Native Method)
07-05 21:42:02.814: E/AndroidRuntime(16955): Caused by: java.lang.NullPointerException
07-05 21:42:02.814: E/AndroidRuntime(16955):    at com.test.WishListManagerActivity.setupListView(WishListManagerActivity.java:51)
07-05 21:42:02.814: E/AndroidRuntime(16955):    at com.test.WishListManagerActivity.onStart(WishListManagerActivity.java:44)
07-05 21:42:02.814: E/AndroidRuntime(16955):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
07-05 21:42:02.814: E/AndroidRuntime(16955):    at android.app.Activity.performStart(Activity.java:3781)
07-05 21:42:02.814: E/AndroidRuntime(16955):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
07-05 21:42:02.814: E/AndroidRuntime(16955):    ... 11 more
like image 377
MayoMan Avatar asked Jul 05 '12 15:07

MayoMan


1 Answers

I have my ORMlite service working fine. All CRUD operations work fine. However when I add a new nullable field to my entity, the whole thing fails.

So the ORMLite exception seems to me to be pretty helpful:

java.sql.SQLException: Unknown field 'newField' from the Android sqlite cursor,
    not in:[desc, name, id]

You are trying to extract the newField field but it is not in the database table. This indicates to me that you did not update the schema of the database. The only fields it has in the table are desc, name, and id.

Either the code that is upgrading the database table has not been run or it is not working correctly. I would take a look at the documentation page that I wrote about how to upgrade the schema under Android.

Unfortunately, Sqlite supports only a few ALTER TABLE commands but ADD COLUMN is one of them.

like image 163
Gray Avatar answered Sep 19 '22 12:09

Gray