简体   繁体   English

线程“main”中的异常java.lang.NullPointerException - JSon to Java

[英]Exception in thread “main” java.lang.NullPointerException - JSon to Java

I am trying to parse JSON code into JAVA through GSON. 我试图通过GSON将JSON代码解析为JAVA。 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 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? 我已经搜索了很多东西,但是找不到可以帮助解决这个问题的问题,我知道为什么NullPointException会发生但不知道它是如何解决它的。是的我看过这个例子 - 什么是NullPointerException,怎么做我修理它? but doesn't really help me much in this context(or it maybe can but am not sure). 但在这种情况下并没有真正帮助我(或者它可能但不确定)。 Thankyou for your time:) 感谢您的时间:)

Based on the code you've published, your array ArrayList <User> child is null. 根据您发布的代码,您的数组ArrayList <User> child为null。 This will cause a NullPointerException the first time you try to access it on this line for (User U : child) { . 这将在您第一次尝试在此行for (User U : child) {上访问它时导致NullPointerException。 You need to initialize the child array something like ArrayList <User> child = new ArrayList<User>() . 您需要初始化子数组,如ArrayList <User> child = new ArrayList<User>() There are a number of other problems with your code as well. 您的代码还存在许多其他问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM