-2

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.

enter image description here

Exception screen shot

enter image description here

modabeckham
  • 227
  • 5
  • 19
  • 1
    you need to initialize that. HashMap> listDataChild=new HashMap>(); – Shadow Mar 31 '15 at 07:56
  • 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) – DaniEll Mar 31 '15 at 07:57

3 Answers3

3

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>>();
Maciej Lach
  • 1,622
  • 3
  • 20
  • 27
  • 1
    Of course this fixes the NullPointerException but there is no background-information! See [this good question](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) for more insight... – DaniEll Mar 31 '15 at 07:59
  • thanks for the answers, it really worked. I got new problem I have commented that. it will be great if u could take a look at it. – modabeckham Mar 31 '15 at 08:06
  • `List listDataHeader;` is also uninitialized thus `.addAll(..)` in line #96 results in NullPointerException. – Maciej Lach Mar 31 '15 at 08:14
3

Your HashMap (listDataChild) is not initialized on line 3.

Initialize it before putting values to it. If you don't initialize it, it will be NULL when you call the method put on it and all operations on NULL will give you a NullPointerException.

For example you could add initialization before calling the put method:

listDataChild = new HashMap<String, List<String>>();
DaniEll
  • 1,022
  • 4
  • 22
  • 31
1

I don't see, that you instantiate the listDataChild object, this will cause this, object is null at the point you want to add a new item.

HashMap<String, List<String>> listDataChild = new HashMap<String, List<String>>();
Ondrej Führer
  • 449
  • 5
  • 9
  • thanks for the answers, it really worked. I got the answer 1 early answer than yours. I got new problem I have commented that. it will be great if u could take a look at it. – modabeckham Mar 31 '15 at 08:05