0

I wrote the below code split a String and insert it into a Object Array. But,

I am getting null pointer exception at the below line. Can you please let me know what is wrong in my code. Thanks in advance

                   nDLs[i].setDisplayName(entrySplit[1]);

Code:

    public class JsonResponse {

         public static void main(String[] args) {

              String response = "eclac-allfocalpoints@un1.org:ECLAC-All Focal Points|testgrp-icc@un.org:TestGRP-ICC|unon-ictsalerts@un.org:UNON-ICTSAlerts|oict-oict-globalvtc@un87.org:OICT-Focal Points All|";
                NodeDLList nDLList = new NodeDLList();



                System.out.println("RESPONSE::::: " + response);

                String split[] = response.split("\\|");

                NodeDLs[] nDLs = new NodeDLs[split.length];

                for (int i = 0; i < split.length; i++) {

                    String entry = split[i];

                    String entrySplit[] = entry.split("\\:");


                       nDLs[i].setDisplayName(entrySplit[1]);
                       nDLs[i].setPrimaryEmail(entrySplit[0]);

                }

                System.out.println("Complete response in Node List Object ::::: " + nDLs);

                nDLList.setdistributionLists(nDLs);

                System.out.println(nDLList);


    }
    }

NodeDLList.java

    public class NodeDLList {



        NodeDLs[] distributionLists = null;



        public NodeDLs[] getdistributionLists() {
            return distributionLists;
        }

        public void setdistributionLists(NodeDLs[] distributionLists) {
            this.distributionLists = distributionLists;
        }


    }

NodeDLs.java

    public class NodeDLs {

        String primaryEmail = null;

        String displayName = null;

        public String getPrimaryEmail() {
            return primaryEmail;
        }

        public void setPrimaryEmail(String primaryEmail) {
            this.primaryEmail = primaryEmail;
        }

        public String getDisplayName() {
            return displayName;
        }

        public void setDisplayName(String displayName) {
            this.displayName = displayName;
        }
  • 1
    Arrays are initialized with `null` for all entries, and you never assigned an instance of `NodeDLs` to `nDLs[i]` before calling a method. – Mark Rotteveel Jun 14 '17 at 14:15
  • @Mark Rotteveel what needs to be done in this case to get rid of null pointer exception? –  Jun 14 '17 at 14:26
  • You need to assign an instance of `NodeDLs` to `nDLs[i]`, specifically: `nDLs[i] = new NodeDLs()` before you call methods on `nDLs[i]`. – Mark Rotteveel Jun 14 '17 at 15:09

0 Answers0