-2

Hello I keep on getting a NullPointerException on item.setProduct(trans.getProduct().getName()); Can you tell why and how? I don't see any reason why would it be null since I've set my adapter in the onPostExecute method. Thanks!

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);
    }
}

here's my log

`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)`
Amrut Bidri
  • 6,276
  • 6
  • 38
  • 80
Menardo
  • 73
  • 7
  • You don't have `Product` in your `trans` most probably. Did you check your `trans` object ? – Bharath Mg Mar 03 '15 at 07:16
  • Maybe `trans.getProduct();` or `trans.getProduct().getName()` is null – Jens Mar 03 '15 at 07:17
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Jens Mar 03 '15 at 07:17
  • Hello Jens and Bharath, I can log the details of `getProduct().getName()` but I can't seem to put it on the `item` object – Menardo Mar 03 '15 at 07:47
  • In `loadListViewData()` what is the `transaction` object? where is it initialised? same question for `item`. – T.Gounelle Mar 03 '15 at 08:26

2 Answers2

0

here's a follow up: (the SalesItem class is on a different file)

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;
}

}

the declaration below is located inside the parent class, so they are declared globally

private ArrayList<SalesItem> transaction;
private SalesItem item;
Menardo
  • 73
  • 7
0

You have a problem with item. You create one new SalesItem() and then you loop over the transaction list. It means that after the first SalesItem added to transaction, you will modify the SalesItem already in transaction, and add it again. Surely, this is not the intended behaviour. The solution is to move new SalesItem() inside the while loop.

For the NullPointerException, add a check for each step where you can have null and log an explicit error, if you want to proceed for next element in list, or do an assert or log with Log.wtf(...) if it should not happen:

public static final String MY_ACTIVITY = "MyActivity";

public void loadListViewData(TransactionObject transObj) {

        //item = new SalesItem(); moved inside loop

        Iterator<Transaction> transactionList = transObj.getTransactions().iterator();
        Transaction trans = null;

        while (transactionList.hasNext()) {
            trans = transactionList.next();
            if (tran==null) {
               Log.e(MY_ACTIVITY, "trans is null");
               continue;
            }
            if (tran.getProduct()==null) {
               Log.e(MY_ACTIVITY, "Product is null for transId: "+trans.getId());
               continue;
            }
            if (tran.getUser()==null) {
               Log.e(MY_ACTIVITY, "User is null for transId: "+trans.getId();
               continue;
            }
            item = new SalesItem();

            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);
        }

    }
T.Gounelle
  • 5,953
  • 1
  • 22
  • 32