I'm trying to add list of string to HashMap, I have shown below the way I'm trying to do. But I'm getting null point exception (I have commented in the code where I'm getting the exception). Can anyone spot what is the mistake I'm making.
private List<Product> mCartList;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
List<String> listdescription = new ArrayList<String>();
List<String> listcrust = new ArrayList<String>();
List<String> listsize = new ArrayList<String>();
String descriptionString;
String crustString;
String sizeString;
for (int i = 0; i < mCartList.size(); i++) {
mCartList.get(i).selected = false;
hot_number = mCartList.size();
if (mCartList.get(i).description != null
&& !mCartList.get(i).description.isEmpty()) {
descriptionString = mCartList.get(i).description;
listdescription.add(descriptionString);
System.out.println("listdescription = " + listdescription);
}
if (mCartList.get(i).crust != null
&& !mCartList.get(i).crust.isEmpty()) {
crustString = mCartList.get(i).crust;
listcrust.add(crustString);
System.out.println("listcrust = " + listcrust);
}
if (mCartList.get(i).size != null
&& !mCartList.get(i).size.isEmpty()) {
sizeString = mCartList.get(i).size;
listsize.add(sizeString);
System.out.println("listsize = " + listsize);
}
}
listDataChild.put("listcrust", listcrust); // error occurs here
listDataChild.put("listsize", listsize);
listDataHeader.addAll(listdescription);
System.out.println("listDataChild = " + listDataChild);
System.out.println("listDataHeader = " + listDataHeader);
I have debugged and got the value for listsize
in 88 line, but when I pass the line 93 the it gives the below mentioned exception.
Exception screen shot
You haven't assigned object to the listDataChild member.
It looks at these are defined on class level:
private List<Product> mCartList;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
And reset of the code is inside a method. Is this right?
By default the member is not initialized and set to null. In case you are trying to perform an operation on a null reference, it fails with the NullPointerException.
Try the following:
HashMap<String, List<String>> listDataChild = new HashMap<String, List<String>>();
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加