1

I am trying to parse JSON code into JAVA through GSON. I have made a similar example before and it worked perfectly but when I try to implement in my program it gives the error :

'Exception in thread "main" java.lang.NullPointerException
at personality.Child.toString(Child.java:25)
at java.lang.String.valueOf(String.java:2994)
at java.io.PrintStream.println(PrintStream.java:821)
at personality.Personality.main(Personality.java:48)

' Really stuck on this, I have done research and still can't find a clear solution. So I am asking you guys.

My code:

Personality Main Class

public class Personality {

        public static void main(String[] args) throws IOException {
            // TODO code application logic her

        Gson gson = new Gson();

        Child C = gson.fromJson(new FileReader("user.json"), Child.class);

        System.out.println(C);
    }
    }

User Class

    public class User {
        public String category;
        public String id;
        public String name;
        public double percentage;
        public double sampling_error;

        //Constructor 
        public User(String category, String id, String name , double percentage, double sampling_error) {

            this.category = category;
            this.id = id;
            this.name = name;
                    this.percentage = percentage;
                    this.sampling_error = sampling_error;
        }    
         @Override
        public String toString() {          
            return "Category " + category + " : id = " + id + " name = " + name;
        }
    }

Child Class

 public class Child {
        public ArrayList <User> child; //Assign arraylist User to Child class


        // toString override - not related to JSON processing, just included for debugging purposes
        @Override
        public String toString() {

            StringBuilder A = new StringBuilder();


            A.append("Number of childrens = " + child.size() + "\n"); //Error points to here

            for (User U : child) {

                A.append(U.toString() + "\n"); //assigns Planet To string to 
            }

            return A.toString();
        }

Json Code

   {   
"children": [
                  {
                    "category": "personality",
                    "id": "Adventurousness",
                    "name": "Adventurousness",
                    "percentage": 0.7871810497070462,
                    "sampling_error": 0.0535169534
                  },
                  {
                    "category": "personality",
                    "id": "Emotionality",
                    "name": "Emotionality",
                    "percentage": 0.9806198117683198,
                    "sampling_error": 0.0500385256
                  },
                  {
                    "category": "personality",
                    "id": "Liberalism",
                    "name": "Authority-challenging",
                    "percentage": 0.9958295966962101,
                    "sampling_error": 0.08735455339999999
                  }
                ]
}

I have searched many things but cannot find questions that can help be solve this , I knid of know why NullPointException occurs but don't know how it solve it.And yes I have looked at this example - What is a NullPointerException, and how do I fix it? but doesn't really help me much in this context(or it maybe can but am not sure). Thankyou for your time:)

Community
  • 1
  • 1
Alia Su
  • 45
  • 1
  • 2
  • 9
  • `child` field in `Child` class which the name doesn't suit it since 'child' is of type User is `null`. Whereas the NullPointerException. So, I think you have a problem in your mapping. I don't see `children` field in `Child` class. – davidxxx Nov 15 '16 at 23:15

1 Answers1

0

Based on the code you've published, your array ArrayList <User> child is null. This will cause a NullPointerException the first time you try to access it on this line for (User U : child) {. You need to initialize the child array something like ArrayList <User> child = new ArrayList<User>(). There are a number of other problems with your code as well.

StvnBrkdll
  • 3,924
  • 1
  • 24
  • 31