Null point exception when trying to add a List<String> to HashMap

modabeckham

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

Maciej Lach

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]

編集
0

コメントを追加

0

関連記事

分類Dev

Null point exception when looking up a Map in java.

分類Dev

Argument Null Exception error when trying to create a Text File

分類Dev

Moq - Null reference exception when trying to test ActionResult

分類Dev

Unexpected exception being raised when trying to convert an input string to float

分類Dev

Android Null point exception error

分類Dev

How to handle null exception when converting from object to string?

分類Dev

Trying to ADD numbers to a string

分類Dev

Transform HashMap<String, String> to HashMap<String, List<String>>()

分類Dev

Null-Pointer Exception when trying to access an EditText from another activity in Kotlin

分類Dev

Null-Pointer Exception when trying to access an EditText from another activity in Kotlin

分類Dev

Nuget Package - feed (VSTS) :Exception 'System.AggregateException' thrown when trying to add source

分類Dev

Nuget Package - feed (VSTS) :Exception 'System.AggregateException' thrown when trying to add source

分類Dev

Nuget Package - feed (VSTS) :Exception 'System.AggregateException' thrown when trying to add source

分類Dev

Why i'm getting "titlebarViewController not supported for this window style" exception when trying to add title bar accessory view

分類Dev

List <HashMap <String、String >> nearbyPlacesListの意味

分類Dev

Cannot convert Int32 to null when trying to add a row to table

分類Dev

Exception when trying to access Resources in jar file

分類Dev

NullPointer Exception When Trying to Update Sprite

分類Dev

read_excel correctly imports file, but "invalid multibyte string" error when trying to put it on a list

分類Dev

Why am i getting this error when i am trying to insert a string into a list?

分類Dev

Console shows "List`1[System.String]" when trying to read particular JSON field

分類Dev

HashMap <String、String>をHashMap <String、List <String >>()に変換します

分類Dev

Navigator showing at 1970 when first point is null

分類Dev

Getting Null Context when trying to retrieve PackageInfo

分類Dev

Null pointer when trying to access POJO attribute

分類Dev

trying to add and display hyperlink to string property of an object

分類Dev

HashMap <String、List <String >> java 8の収集

分類Dev

HashMap <String、List <String >>を保存/保存する方法

分類Dev

iterate a List<HashMap<String, String>> values using java

Related 関連記事

  1. 1

    Null point exception when looking up a Map in java.

  2. 2

    Argument Null Exception error when trying to create a Text File

  3. 3

    Moq - Null reference exception when trying to test ActionResult

  4. 4

    Unexpected exception being raised when trying to convert an input string to float

  5. 5

    Android Null point exception error

  6. 6

    How to handle null exception when converting from object to string?

  7. 7

    Trying to ADD numbers to a string

  8. 8

    Transform HashMap<String, String> to HashMap<String, List<String>>()

  9. 9

    Null-Pointer Exception when trying to access an EditText from another activity in Kotlin

  10. 10

    Null-Pointer Exception when trying to access an EditText from another activity in Kotlin

  11. 11

    Nuget Package - feed (VSTS) :Exception 'System.AggregateException' thrown when trying to add source

  12. 12

    Nuget Package - feed (VSTS) :Exception 'System.AggregateException' thrown when trying to add source

  13. 13

    Nuget Package - feed (VSTS) :Exception 'System.AggregateException' thrown when trying to add source

  14. 14

    Why i'm getting "titlebarViewController not supported for this window style" exception when trying to add title bar accessory view

  15. 15

    List <HashMap <String、String >> nearbyPlacesListの意味

  16. 16

    Cannot convert Int32 to null when trying to add a row to table

  17. 17

    Exception when trying to access Resources in jar file

  18. 18

    NullPointer Exception When Trying to Update Sprite

  19. 19

    read_excel correctly imports file, but "invalid multibyte string" error when trying to put it on a list

  20. 20

    Why am i getting this error when i am trying to insert a string into a list?

  21. 21

    Console shows "List`1[System.String]" when trying to read particular JSON field

  22. 22

    HashMap <String、String>をHashMap <String、List <String >>()に変換します

  23. 23

    Navigator showing at 1970 when first point is null

  24. 24

    Getting Null Context when trying to retrieve PackageInfo

  25. 25

    Null pointer when trying to access POJO attribute

  26. 26

    trying to add and display hyperlink to string property of an object

  27. 27

    HashMap <String、List <String >> java 8の収集

  28. 28

    HashMap <String、List <String >>を保存/保存する方法

  29. 29

    iterate a List<HashMap<String, String>> values using java

ホットタグ

アーカイブ