我使用JSON
和数据库来获取一些值并将它们设置为listviews。 JSON
个对象没问题但是当我尝试从数据库(游标对象)获取值并将它们设置为ArrayList时,它会给出NullPointerException
。
ArrayList<String> stringName,stringAddress,stringCapacity;
...
...
...
cursor_restnames = db.getRestNames("Ankara");
if (cursor_restnames.moveToFirst()) {
for (cursor_restnames.moveToFirst(); !cursor_restnames.isAfterLast(); cursor_restnames.moveToNext()) {
System.out.println("aaaaa "+cursor_restnames.getString(cursor_restnames.getColumnIndex("name")).toString()); // I can see the cursor object in logcat which is "aspava".
stringName.add(cursor_restnames.getString(cursor_restnames.getColumnIndex("name")).toString()); // I got error here which is NullPointerException but as I mentioned it is not null, it has "aspava" string.
}
cursor_restnames.close();
}
我的DBadapter有 getRestNames 功能,即
public Cursor getRestNames(String city) {
Cursor myCursor = db.rawQuery("select name from rest where city='"+city+"'", null);
if (myCursor != null) {
myCursor.moveToFirst();
}
return myCursor;
}
如果这些代码段无效,我可以写下整个项目。 感谢您的帮助,如果我的英语有任何错误,我很抱歉。
答案 0 :(得分:2)
你是否初步确定了你的Arraylist
stringName = new Arraylist();
答案 1 :(得分:1)
您应该初始化数组列表,因为这样您的对象值为null
ArrayList<String> stringName = new ArrayList<String>();