Java 创建递归树时出现运行时错误(空点异常)

Java 创建递归树时出现运行时错误(空点异常),java,json,jtree,Java,Json,Jtree,我希望将以下内容作为Json输入,并将其转换为java中的树数据结构 { "component": "A", "status": 0, "children": [ { "component": "AA",

我希望将以下内容作为Json输入,并将其转换为java中的树数据结构

                {
                    "component": "A",
                    "status": 0,
                    "children": [
                        {
                            "component": "AA",
                            "status": 0,
                            "children": [
                                {
                                    "component": "AAA",
                                    "status": 0,
                                    "children": []
                                },
                                {
                                    "component": "AAB",
                                    "status": 0,
                                    "children": []
                                }
                            ]
                        },
                        {
                            "component": "AB",
                            "status": 0,
                            "children": [
                                {
                                    "component": "ABA",
                                    "status": 0,
                                    "children": []
                                },
                                {
                                    "component": "ABB",
                                    "status": 0,
                                    "children": []
                                }
                            ]
                        }
                }
我已经编写了以下代码,但是如果有人能够找到错误,它会显示运行时错误。for循环一直执行到节点通过递归没有子节点为止,而它应该返回显示空指针异常的位置

            import java.io.FileNotFoundException;
            import java.io.FileReader;
            import java.io.IOException;
            import java.util.List;
            import java.io.BufferedReader;
            import org.json.*;

            public class Sample {

                public static void main(String[] args) {

                        BufferedReader in = new BufferedReader( new FileReader("json.txt") );
                        StringBuilder builder = new StringBuilder();
                        String line;
                        while ( ( line = in.readLine() ) != null ) {
                            builder.append(line);

                        object = new JSONObject( builder.toString() );
                        imlementation im = new imlementation();
                        im.createnode(object);                                                      
                }                   
            }
            public class node {
                    public String component;
                    public int status;
                    public List<node> children;
            }

            import org.json.JSONArray;
            import org.json.JSONException;
            import org.json.JSONObject;

            public class imlementation {

                public node createnode(JSONObject ob)
                {
                    node n = new node();
                    try
                    {
                        JSONArray children = ob.getJSONArray("children");
                        String component = ob.getString("component");
                        int status = ob.getInt("status");

                        n.component = component;
                        n.status = status;
                        n.children=null;
                        System.out.println( "component " + component + "status " + status );

                        int i;
                        for(i=0;i<children.length();i++)
                        {
                            n.children.add( createnode( children.getJSONObject( i ) ) );
                        }
                        return n;

                    }
                    catch ( JSONException ex ) 
                    {
                        ex.printStackTrace();
                    }

                }

                }
你已经准备好了

n.children=null;
然后在你正在做的for循环中

n.children.add(....)
这可能导致空指针异常

你可能需要这样做

n.children = new List<Node>()
n.children=新列表()
在进行添加之前


另一个潜在原因是,在某些递归调用期间,子变量可能为null。children.length将导致空指针异常。

请给出异常StackTrace请添加堆栈跟踪,并在哪一行获取NPE。
n.children = new List<Node>()