首页 新闻 会员 周边 捐助

我哪里做错了?

0
[已解决问题] 解决于 2016-04-14 14:14
 1 package per.swwfourteen.fourteen;
 2 
 3 public class Student {
 4     private String id;
 5 
 6     private String name;
 7 
 8     private String password;
 9 
10     private String age;
11 
12     public void setId(String id) {
13         this.id = id;
14     }
15 
16     public String getId() {
17         return this.id;
18     }
19 
20     public void setName(String name) {
21         this.name = name;
22     }
23 
24     public String getName() {
25         return this.name;
26     }
27 
28     public void setPassword(String password) {
29         this.password = password;
30     }
31 
32     public String getPassword() {
33         return this.password;
34     }
35 
36     public void setAge(String age) {
37         this.age = age;
38     }
39 
40     public String getAge() {
41         return this.age;
42     }
43 
44 }

 1 package per.swwfourteen.fourteen;
 2 
 3 public class Teacher {
 4     private String id;
 5 
 6     private String name;
 7 
 8     private String password;
 9 
10     private String age;
11 
12     private Car car;
13 
14     public void setId(String id) {
15         this.id = id;
16     }
17 
18     public String getId() {
19         return this.id;
20     }
21 
22     public void setName(String name) {
23         this.name = name;
24     }
25 
26     public String getName() {
27         return this.name;
28     }
29 
30     public void setPassword(String password) {
31         this.password = password;
32     }
33 
34     public String getPassword() {
35         return this.password;
36     }
37 
38     public void setAge(String age) {
39         this.age = age;
40     }
41 
42     public String getAge() {
43         return this.age;
44     }
45 
46     public void setCar(Car car) {
47         this.car = car;
48     }
49 
50     public Car getCar() {
51         return this.car;
52     }
53 
54 }

 1 package per.swwfourteen.fourteen;
 2 
 3 public class Car {
 4     private String num;
 5 
 6     public void setNum(String num) {
 7         this.num = num;
 8     }
 9 
10     public String getNum() {
11         return this.num;
12     }
13 
14 }
 1 package per.swwfourteen.fourteen;
 2 
 3 import java.util.List;
 4 
 5 public class Root_List {
 6     private List<Student> student;
 7 
 8     private List<Teacher> teacher;
 9 
10     public void setStudent(List<Student> student) {
11         this.student = student;
12     }
13 
14     public List<Student> getStudent() {
15         return this.student;
16     }
17 
18     public void setTeacher(List<Teacher> teacher) {
19         this.teacher = teacher;
20     }
21 
22     public List<Teacher> getTeacher() {
23         return this.teacher;
24     }
25 
26 }
 1 package per.swwfourteen.fourteen;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.File;
 5 import java.io.FileReader;
 6 import java.io.IOException;
 7 
 8 import com.alibaba.fastjson.JSONObject;
 9 
10 public class JsonRoot {
11     public static void main(String[] args)throws Exception{
12         Root_List root=JSONObject.parseObject(ReadRoot("D:"+File.separator+"1.json"),Root_List.class);
13         for(Student s:root.getStudent()){
14             System.out.println(s.getId());
15             System.out.println(s.getName());
16             System.out.println(s.getAge());
17             System.out.println(s.getPassword());
18         }
19         for(Teacher t:root.getTeacher()){
20             System.out.println(t.getId());
21             System.out.println(t.getName());
22             System.out.println(t.getAge());
23             System.out.println(t.getPassword());
24             System.out.println(t.getCar());
25         }
26     }
27     
28     public static String ReadRoot(String rer)throws IOException{
29         StringBuffer sb=new StringBuffer();
30         BufferedReader br=new BufferedReader(new FileReader(rer));
31         char[] it=new char[1024];
32         int sum=0;
33         while((sum=br.read(it))!=-1){
34             String s=String.valueOf(it, 0, sum);
35             sb.append(s);
36         }
37         br.close();
38         return sb.toString();
39     }
40 }

运行后提示13行报错,

Exception in thread "main" java.lang.NullPointerException
at per.swwfourteen.fourteen.JsonRoot.main(JsonRoot.java:13)

 1 {
 2   "Root": {
 3     "student": [
 4       {
 5         "id": "001",
 6         "name": "student1",
 7         "password": "123",
 8         "age": "20"
 9       },
10       {
11         "id": "002",
12         "name": "student2",
13         "password": "456",
14         "age": "21"
15       },
16       {
17         "id": "003",
18         "name": "student3",
19         "password": "123",
20         "age": "21"
21       }
22     ],
23     "teacher": [
24       {
25         "id": "001",
26         "name": "teacher1",
27         "password": "123",
28         "age": "20",
29         "car": { "num": "098" }
30       },
31       {
32         "id": "002",
33         "name": "teacher2",
34         "password": "123",
35         "age": "20",
36         "car": { "num": "098" }
37       }
38     ]
39   }
40 }

 

 
师太来老道这坐坐的主页 师太来老道这坐坐 | 初学一级 | 园豆:34
提问于:2016-04-14 09:48
< >
分享
最佳答案
0

json转成对象后,root是个map类吧?你用root.get("student")试试,或者遍历keySet集合

奖励园豆:5
回看欧洲 | 菜鸟二级 |园豆:208 | 2016-04-14 10:06

是List

师太来老道这坐坐 | 园豆:34 (初学一级) | 2016-04-14 10:09

root. 只能点出getClass,和getStudentList以及getTeacherList

师太来老道这坐坐 | 园豆:34 (初学一级) | 2016-04-14 10:10

@略知闻风雨: for student的循环注释掉,teacher循环没问题吗

回看欧洲 | 园豆:208 (菜鸟二级) | 2016-04-14 10:15

@Laughing_Lz: 有= =,一模一样

师太来老道这坐坐 | 园豆:34 (初学一级) | 2016-04-14 10:17

@略知闻风雨: 把Root_List root=JSONObject.parseObject(ReadRoot("D:"+File.separator+"1.json"),Root_List.class);

改成 Map root  = JSONObject.parseObject(ReadRoot("D:"+File.separator+"1.json"),Root_List.class);

然后试试root.get("student");

回看欧洲 | 园豆:208 (菜鸟二级) | 2016-04-14 10:19

@略知闻风雨: 给你跑了一遍才看出来。你的json数据错了,你带个Root有什么用,删掉它和它的{},再跑就OK了

回看欧洲 | 园豆:208 (菜鸟二级) | 2016-04-14 11:08

@略知闻风雨: 

 1  {
 2      "student": [
 3        {
 4         "id": "001",
 5         "name": "student1",
 6          "password": "123",
 7          "age": "20"
 8        },
 9        {
10          "id": "002",
11          "name": "student2",
12          "password": "456",
13          "age": "21"
14        },
15        {
16          "id": "003",
17          "name": "student3",
18          "password": "123",
19          "age": "21"
20       }
21     ],
22      "teacher": [
23        {
24          "id": "001",
25          "name": "teacher1",
26          "password": "123",
27          "age": "20",
28          "car": { "num": "098" }
29        },
30        {
31          "id": "002",
32          "name": "teacher2",
33          "password": "123",
34          "age": "20",
35          "car": { "num": "098" }
36        }
37     ]
38 
39  }
回看欧洲 | 园豆:208 (菜鸟二级) | 2016-04-14 11:09

@Laughing_Lz: ....我删了还报= =

师太来老道这坐坐 | 园豆:34 (初学一级) | 2016-04-14 11:11

@略知闻风雨: 

1         Gson gson = new Gson();
2         Root_List root = gson.fromJson(ReadRoot("D:"+File.separator+"1.json"), Root_List.class);

换掉alibaba的吧···我这用的Gson没问题

回看欧洲 | 园豆:208 (菜鸟二级) | 2016-04-14 11:16

@略知闻风雨: json转对象别用alibba的包,换gson,这个http://download.csdn.net/detail/zxyyxzzxy004/8206489

回看欧洲 | 园豆:208 (菜鸟二级) | 2016-04-14 11:20

@Laughing_Lz: 这个没用过- -

师太来老道这坐坐 | 园豆:34 (初学一级) | 2016-04-14 11:30

@Laughing_Lz: 去掉大括号弄好了,太坑了做了一个别的项目它自己就好了

师太来老道这坐坐 | 园豆:34 (初学一级) | 2016-04-14 13:47
其他回答(3)
0
public List<Student> getStudent() 
{
    if(this.student==null)return new List<Student>();
     return this.student; 
}

 

吴瑞祥 | 园豆:29449 (高人七级) | 2016-04-14 10:04

new List<Student>();

这块儿报错..

支持(0) 反对(0) 师太来老道这坐坐 | 园豆:34 (初学一级) | 2016-04-14 10:11

@略知闻风雨: .我又不会写Java代码..只是说思路.

问题是因为你对null 循环了.只要返回一个空数组出去就好了.

支持(0) 反对(0) 吴瑞祥 | 园豆:29449 (高人七级) | 2016-04-14 10:12
0

空对象,你调试一下,root中没对象,读json可能有问题

小光 | 园豆:1766 (小虾三级) | 2016-04-14 10:14

d一个for循环都进不去

支持(0) 反对(0) 师太来老道这坐坐 | 园豆:34 (初学一级) | 2016-04-14 10:15

@略知闻风雨: 肯定啊,空对象,就是你的root中集合为空,取不到student对象,我是让你看第十二行

 Root_List root=JSONObject.parseObject(ReadRoot("D:"+File.separator+"1.json"),Root_List.class);

这里面的root中有数据没,应该是没有,解析json获得数据为空
,你看下是不是

支持(0) 反对(0) 小光 | 园豆:1766 (小虾三级) | 2016-04-14 10:21

@博客园乄小光: 是的,student和teacher都是null

支持(0) 反对(0) 师太来老道这坐坐 | 园豆:34 (初学一级) | 2016-04-14 10:22

@略知闻风雨: 还是读json,你看一下路径,读json的逻辑是不是有问题,往上一步调试,进到ReadRoot方法里面,一步一步找

支持(0) 反对(0) 小光 | 园豆:1766 (小虾三级) | 2016-04-14 10:26

@博客园乄小光: 纳闷了,我student类里是有东西的啊,怎么就null了呢

支持(0) 反对(0) 师太来老道这坐坐 | 园豆:34 (初学一级) | 2016-04-14 10:34

@略知闻风雨: 跟你student类没关系,要不就是路径错了,要不就是读json逻辑错了,读不到数据,就为空

支持(0) 反对(0) 小光 | 园豆:1766 (小虾三级) | 2016-04-14 10:43

@略知闻风雨: 我现在有任务,要不然帮你把代码弄出来了

支持(0) 反对(0) 小光 | 园豆:1766 (小虾三级) | 2016-04-14 10:43

@略知闻风雨: 不要方,仔细点调试,一行一行观察读的数据,是不是读出来了,是不是处理对了

支持(0) 反对(0) 小光 | 园豆:1766 (小虾三级) | 2016-04-14 10:45

@博客园乄小光: 方的不行,这个是我自己练手的,不着急,我慢慢找抽空帮我感激不尽,读到Root_List root=JSONObject.parseObject(ReadRoot("D:"+File.separator+"1.json"),Root_List.class);

发现student和teacher获取到的都是null;然后for循环就不用想了,感觉应该是ReadRoot的问题,但是ReadRoot运行完无异常- -

支持(0) 反对(0) 师太来老道这坐坐 | 园豆:34 (初学一级) | 2016-04-14 10:50

@略知闻风雨: 无异常和无数据不冲突,语法没问题肯定无异常。。。。。。你把ReadRoot里面代码全删了也无异常。。。。,还是慢慢弄读json的代码

支持(0) 反对(0) 小光 | 园豆:1766 (小虾三级) | 2016-04-14 11:02

@博客园乄小光: 

{
    "student": [
        {
            "id": "001", 
            "name": "student1", 
            "password": "123", 
            "age": "20"
        }, 
        {
            "id": "002", 
            "name": "student2", 
            "password": "456", 
            "age": "21"
        }, 
        {
            "id": "003", 
            "name": "student3", 
            "password": "123", 
            "age": "21"
        }
    ], 
    "teacher": [
        {
            "id": "001", 
            "name": "teacher1", 
            "password": "123", 
            "age": "20", 
            "car": {
                "num": "098"
            }
        }, 
        {
            "id": "002", 
            "name": "teacher2", 
            "password": "123", 
            "age": "20", 
            "car": {
                "num": "098"
            }
        }
    ]
}

为什么去掉大括号就好了呢?Root不是在xml中是根节点么?难道在json里就不要了?

支持(0) 反对(0) 师太来老道这坐坐 | 园豆:34 (初学一级) | 2016-04-14 13:48

@略知闻风雨: 。。。

支持(0) 反对(0) 小光 | 园豆:1766 (小虾三级) | 2016-04-15 13:42
0

建议在可能为空的情况下,一定要做为空判断。一般合格的java代码都应该这样处理

夜深nps | 园豆:202 (菜鸟二级) | 2016-04-14 11:40

现在读不到数据呀

支持(0) 反对(0) 师太来老道这坐坐 | 园豆:34 (初学一级) | 2016-04-14 12:20
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册