When I add a new field, I get a null pointer

My ORMlite service works for me. All CRUD operations are working fine. However, when I add a new field with a null value for my object, all this 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 database and recreate it, everything will be fine. I would like to know why this is a simple circuit change? I think ahead, where the database is filled with data, and I do not want a new field with a zero value to break it, or can this 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 that I get when I update an entity class to include the next new field.

 @DatabaseField( canBeNull = true ) private String newField; 

Now, seeing that if I delete the new field, the test file / application works fine, then this code breaks everything.

 [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 
+6
source share
1 answer

My ORMlite service works for me. All CRUD operations are working fine. However, when I add a new field with a null value for my entity, all of this fails.

So the ORMLite exception looks very useful:

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

You are trying to retrieve the newField field, but it is not in the database table. This indicates that you have not updated the database schema. The only fields it has in the table are desc , name and id .

Either the code that updates the database table was not running, or it does not work correctly. I would take a look at the documentation page that I wrote about how to update the schema for Android .

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

+6
source

Source: https://habr.com/ru/post/919779/


All Articles