您好我继续在item.setProduct(trans.getProduct().getName());
上获取NullPointerException
你能说出原因和方法吗?我没有看到任何理由为什么它会为null因为我已经在onPostExecute方法中设置了我的适配器。谢谢!
private class TransactionTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
BackendService service = BackendAPI.serviceApi();
TransactionObject transactionList;
Log.e("AsyncTask", "I'm called");
try {
transactionList = service.listTransactions(15);
if (transactionList != null) {
Log.e(TAG, transactionList.toString());
loadListViewData(transactionList);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void loadListViewData(TransactionObject transObj) {
item = new SalesItem();
Iterator<Transaction> transactionList = transObj.getTransactions().iterator();
Transaction trans = null;
while (transactionList.hasNext()) {
trans = transactionList.next();
item.setProduct(trans.getProduct().getName());
item.setCost(trans.getCost());
item.setName(trans.getUser().getName());
item.setDate(trans.getCreated_at());
item.setId(trans.getId());
transaction.add(item);
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
adapter = new SalesListAdapter(getActivity(), transaction);
listview.setAdapter(adapter);
}
}
这是我的日志
`03-03 07:40:52.604 2011-2033/net.payswitch.switchmobile W/System.err﹕ java.lang.NullPointerException
03-03 07:40:52.604 2011-2033/net.payswitch.switchmobile W/System.err﹕ at net.payswitch.switchmobile.fragments.SalesDataFragment$TransactionTask.loadListViewData(SalesDataFragment.java:188)
03-03 07:40:52.604 2011-2033/net.payswitch.switchmobile W/System.err﹕ at net.payswitch.switchmobile.fragments.SalesDataFragment$TransactionTask.doInBackground(SalesDataFragment.java:166)
03-03 07:40:52.604 2011-2033/net.payswitch.switchmobile W/System.err﹕ at net.payswitch.switchmobile.fragments.SalesDataFragment$TransactionTask.doInBackground(SalesDataFragment.java:152)
03-03 07:40:52.604 2011-2033/net.payswitch.switchmobile W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-03 07:40:52.604 2011-2033/net.payswitch.switchmobile W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-03 07:40:52.604 2011-2033/net.payswitch.switchmobile W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-03 07:40:52.604 2011-2033/net.payswitch.switchmobile W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-03 07:40:52.604 2011-2033/net.payswitch.switchmobile W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-03 07:40:52.604 2011-2033/net.payswitch.switchmobile W/System.err﹕ at java.lang.Thread.run(Thread.java:856)`
答案 0 :(得分:0)
这是一个跟进:( SalesItem类在不同的文件上)
public class SalesItem {
private String product;
private String cost;
private String name;
private String date;
private String id;
public String getProduct() {
return product;
}
public void setProduct(String product) {
this.product = product;
}
public String getCost() {
return cost;
}
public void setCost(String cost) {
this.cost = cost;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
下面的声明位于父类中,因此它们被全局声明
private ArrayList<SalesItem> transaction;
private SalesItem item;
答案 1 :(得分:0)
item
出现问题。您创建一个new SalesItem()
,然后循环遍历事务列表。这意味着在第一个SalesItem
添加到transaction
之后,您将修改已SalesItem
中的transaction
,并再次添加 。当然,这不是预期的行为。
解决方案是在new SalesItem()
循环中移动while
。
对于NullPointerException
,如果要继续查看列表中的下一个元素,或者执行{{1},请为每个步骤添加一个检查null
并记录显式错误如果不应该发生,请使用assert
进行记录:
Log.wtf(...)