我有asyncTask,在DoInBackground方法中我调用了一个DBHelper类,它应该返回SQLiteDB中的每一行。但是当用于检索值的DBHelper类方法被触发时,我得到错误:
07-20 10:33:33.869:E / AndroidRuntime(22238):致命异常:AsyncTask#5 07-20 10:33:33.869:E / AndroidRuntime(22238):进程:com.test.test,PID:22238 07-20 10:33:33.869:E / AndroidRuntime(22238):java.lang.RuntimeException:执行doInBackground()时发生错误 07-20 10:33:33.869:E / AndroidRuntime(22238):在android.os.AsyncTask $ 3.done(AsyncTask.java:300) 07-20 10:33:33.869:E / AndroidRuntime(22238):at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 07-20 10:33:33.869:E / AndroidRuntime(22238):at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 07-20 10:33:33.869:E / AndroidRuntime(22238):at java.util.concurrent.FutureTask.run(FutureTask.java:242) 07-20 10:33:33.869:E / AndroidRuntime(22238):在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231) 07-20 10:33:33.869:E / AndroidRuntime(22238):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 07-20 10:33:33.869:E / AndroidRuntime(22238):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587) 07-20 10:33:33.869:E / AndroidRuntime(22238):at java.lang.Thread.run(Thread.java:818) 07-20 10:33:33.869:E / AndroidRuntime(22238):引起:java.lang.NullPointerException:尝试调用虚方法' void test.ItemFolderP.setId(java.lang.String)&#39 ;在null对象引用上 07-20 10:33:33.869:E / AndroidRuntime(22238):at test.test.returnValues(testt.java:59) 07-20 10:33:33.869:E / AndroidRuntime(22238):at test.test $ GetFProducts.doInBackground(test.java:107) 07-20 10:33:33.869:E / AndroidRuntime(22238):at test.test $ GetFProducts.doInBackground(test.java:1) 07-20 10:33:33.869:E / AndroidRuntime(22238):在android.os.AsyncTask $ 2.call(AsyncTask.java:288) 07-20 10:33:33.869:E / AndroidRuntime(22238):at java.util.concurrent.FutureTask.run(FutureTask.java:237) 07-20 10:33:33.869:E / AndroidRuntime(22238):... 4更多
这是DBHelper的代码:
public class DBHelper{
private ItemFolderP item; //here I declare my item
这是检索值的方法
public ItemFolderP returnValues(int index){
Cursor mRow= database.rawQuery("select * from " + CM_TABLE + " LIMIT 0, 4", null);
if (null!=mRow) {
mRow.moveToNext();
item.setId(Integer.toString(mRow.getInt(mRow.getColumnIndex("Id"))));
item.setTitle(mRow.getString(mRow.getColumnIndex("test")));
item.setCode(mRow.getString(mRow.getColumnIndex("code")));
item.setPrice(mRow.getString(mRow.getColumnIndex("price")));
item.setImageUrl(mRow.getString(mRow.getColumnIndex("url")));
}
return item;
}
这是项目类:
public class ItemFolderP {
public String id;
public String title;
public String codice;
public String price;
public String imageUrl;
public ItemFolderP(String id, String title, String codice, String price, String imageUrl) {
super();
this.title = title;
this.id = id;
this.price = price;
this.imageUrl = imageUrl;
}
//titolo
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
//id
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
//codice
public String getCode() {
return title;
}
public void setCode(String codice) {
this.title = codice;
}
//image Url
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
//id
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
我觉得错误可能是由于我没有将上下文传递给方法,但我无法理解如何修复它。有人能帮助我吗?
答案 0 :(得分:2)
您未在代码中的任何位置提供item
值,因此始终为null
。你应该替换这个块:
item.setId(Integer.toString(mRow.getInt(mRow.getColumnIndex("Id"))));
item.setTitle(mRow.getString(mRow.getColumnIndex("test")));
item.setCode(mRow.getString(mRow.getColumnIndex("code")));
item.setPrice(mRow.getString(mRow.getColumnIndex("price")));
item.setImageUrl(mRow.getString(mRow.getColumnIndex("url")));
与
item = new ItemFolderP(
Integer.toString(mRow.getInt(mRow.getColumnIndex("Id"))),
mRow.getString(mRow.getColumnIndex("test")),
mRow.getString(mRow.getColumnIndex("code")),
mRow.getString(mRow.getColumnIndex("price")),
mRow.getString(mRow.getColumnIndex("url")));
此外,我认为item
不应该是私有字段,因为它仅在returnValues
方法中使用。一个局部变量就足够了。