当我添加一个新字段时,我得到了一个空指针异常。

7

我的ORMlite服务正常工作,所有CRUD操作都正常。但是当我向实体添加一个新的可空字段时,整个应用程序就崩溃了。我得到了空指针,并且应用程序无法运行。

这是我添加的字段:

@DatabaseField( canBeNull = true  )
private String newField;

如果我删除数据库并重新创建,则一切正常。我想知道的是为什么简单的模式更改会导致这种情况?我正在考虑一个数据库已满数据的情况,我不希望新的可空字段破坏它,或者这是可以预料的吗?
以下是异常信息:
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)

这是我在更新实体类并添加如下新字段时遇到的异常:

以下是需要翻译的内容:

@DatabaseField( canBeNull = true  )
private String newField;

现在,如果我删除新字段,我的测试用例/应用程序就能正常工作,那么就是这段代码导致了问题。

[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

我猜“null pointer”是指空指针异常?如果是的话,请编辑您的帖子以显示完整的异常信息。 - Gray
另外,您能展示一下您如何更新模式的代码吗? - Gray
详细阐述梨形问题。 - eternalmatt
@eternalmatt “goes pear shaped” 在英国意味着“出了大问题”。 - Gray
我的回答有帮到你吗?如果有的话,请确保点赞并接受它。 - Gray
2个回答

6

我的ORMlite服务工作正常,所有的CRUD操作都正常。但是当我向实体中添加一个新的可空字段时,整个过程就失败了。

所以ORMLite异常对我来说似乎非常有帮助:

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

你正在尝试提取newField字段,但它不在数据库表中。这表明您没有更新数据库的架构。表中仅有的字段是descnameid
要么是升级数据库表的代码尚未运行,要么是它没有正常工作。我建议查看我撰写的关于如何在Android下升级架构的文档页面。
不幸的是,Sqlite仅支持少量ALTER TABLE命令,但ADD COLUMN是其中之一。

我在DatabaseHelper类中找不到getHelper()方法,那我该怎么办? - DinhNgocHien

0
我通过升级数据库版本解决了这个问题!你应该知道当你在数据库中添加或删除..时,应该升级数据库版本! 示例:
if the current version is = 2 after the change should be 3 ;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接