我正在从一个表面上正确打开的SQLite数据库中读取数据。但是,当我尝试比较我从中拉出的两个对象的标题时,我得到一个空指针异常,这表明读取过程的编写方式有些不对。果然 - 当我评论比较部分时(按名称按字母顺序对植物列表进行排序),应用程序将运行,但视图中的所有数据字段显示为“null”。我已经多次查看了代码,并且假设我必须做一些逻辑上错误的事情,即另一组眼睛可能能够检测到。
这是带有堆栈跟踪的Logcat输出:
[ 12-03 10:16:57.563 5898:0x1714 E/AndroidRuntime ]
FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:200)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
at java.lang.Thread.run(Thread.java:1019)
Caused by: java.lang.NullPointerException
at net.xxx.wildedibles.full.intro.LoadingFragment$LoadingTask$1.compare(LoadingFragment.java:101)
at net.xxx.wildedibles.full.intro.LoadingFragment$LoadingTask$1.compare(LoadingFragment.java:99)
at java.util.TimSort.countRunAndMakeAscending(TimSort.java:320)
at java.util.TimSort.sort(TimSort.java:199)
at java.util.TimSort.sort(TimSort.java:169)
at java.util.Arrays.sort(Arrays.java:2090)
at java.util.Collections.sort(Collections.java:1965)
at net.xxx.wildedibles.full.intro.LoadingFragment$LoadingTask.doInBackground(LoadingFragment.java:99)
at net.xxx.wildedibles.full.intro.LoadingFragment$LoadingTask.doInBackground(LoadingFragment.java:68)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
... 4 more
这是LoadingFragment的相关部分:
Plant plant = new Plant();
GlobalData.plantArray = plant.populatePlantDataFromDB(mDbHelper);
Collections.sort(GlobalData.plantArray , new Comparator<Plant>(){
public int compare(Plant plant1, Plant plant2) {
return plant1.getTitle().compareToIgnoreCase(plant2.getTitle());
}
});
这是Plant类的相关部分:
//Population method to read from DB
public List<Plant> populatePlantDataFromDB(DBAdapter mDbHelper){
List<Plant> plantsList = new ArrayList<Plant>();
String selectQuery = "SELECT * FROM plants";
Cursor c = mDbHelper.getData(selectQuery);
// looping through all rows and adding to list
c.moveToFirst();
do {
Plant plant = new Plant();
plant.setId(c.getInt((c.getColumnIndex("id"))));
plant.setTitle((c.getString(c.getColumnIndex("title"))));
plant.setScientificName((c.getString(c.getColumnIndex("scientificName"))));
//many, many fields
plantsList.add(plant);
} while (c.moveToNext());
c.close();
return plantsList;
}
如果代码间距有点古怪,我很抱歉,但你可以看到这里发生了什么。
非常感谢你能告诉我的任何事情!
答案 0 :(得分:0)
比较方法的正确实现应该修复你得到的NullPointerException:
Collections.sort(GlobalData.plantArray , new Comparator<Plant>(){
public int compare(Plant plant1, Plant plant2) {
if(plant1 == null || plant2 == null || TextUtils.isEmpty(plant1.getTitle()) || TextUtils.isEmpty(plant2.getTitle()) ){
return 0 ;
}
return plant1.getTitle().compareToIgnoreCase(plant2.getTitle());
}
});
并且对于空值,请确保列的名称正确
答案 1 :(得分:0)
我会测试:
if (c == null) return null;
也是,而不是
getColumnIndex()
我会用:
getColumnIndexOrThrow()
为了测试,我还会覆盖你的植物类中的toString()
并在那里写下id,title和scientificName。然后使用toString()在while循环中记录每个工厂对象,使用toString()并查看缺少的内容。
我还会使用常量(public static final
)作为列名。输入错误很容易。
希望这可以帮助您找到问题。