简体   繁体   English

JSONArray中的JSONArray与Java中的向量

[英]JSONArray in JSONArray with vector in Java

How to put inner JSONArray data into vector? 如何将内部JSONArray数据放入向量中? The program only crash when i want to put data into the vector when I just print the data everythink works fine. 仅当我只想打印数据时,程序才崩溃,我想一切都正常。 Why? 为什么? How to fix this? 如何解决这个问题? When I run the program with vector: 当我使用vector运行程序时:

0
Budapest
Szolnok
time: 0 2018.10.21. 11:20 2018.10.21. 13:25
Exception in thread "main" java.lang.NullPointerException
    at Routes.addTime(Routes.java:20)
    at JSONReader.main(JSONReader.java:30)

And when I run whitout vecor (just print to the console the good output): 当我运行vecorout vecor时(只需将其输出到控制台即可得到良好的输出):

0 0
Budapest 布达佩斯
Szolnok 索尔诺克
time: 0 2018.10.21. 时间:0 2018.10.21 11:20 2018.10.21. 2018年10月21日11:20 13:25 13:25
time: 1 2018.10.21. 时间:1 2018.10.21 13:20 2018.10.21. 2018年10月21日13:20 15:25 15:25
1 1个
Veszprem 维斯普雷姆
Budapest 布达佩斯
time: 0 2018.10.30. 时间:0 2018.10.30。 09:35 2018.10.30. 09:35 2018.10.30 11:02 11:02
2 2
Veszprem 维斯普雷姆
Gyor 杰尔
time: 0 2018.11.10. 时间:0 2018.11.10。 15:46 2018.11.10. 15:46 2018.11.10。 16:50 16:50
1 1个
Gyor 杰尔
Szombathely 僵尸地
time: 0 2018.11.05. 时间:0 2018.11.05。 13:10 2018.11.05. 2018年11月5日13:10 14:50 14:50

JSONReader.java JSONReader.java

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

public class JSONReader {
static String JSON_STRING = "{\"routes\": [{\"id\": 0,\"from\": \"Budapest\",\"to\": \"Szolnok\",\"times\": [{ \"id\": 0, \"start\": \"2018.10.21. 11:20\", \"arrive\": \"2018.10.21. 13:25\" },{ \"id\": 1, \"start\": \"2018.10.21. 13:20\", \"arrive\": \"2018.10.21. 15:25\" }]}, {\"id\": 1,\"from\": \"Veszprem\",\"to\": \"Budapest\",\"times\": [{ \"id\": 0, \"start\": \"2018.10.30. 09:35\", \"arrive\": \"2018.10.30. 11:02\" }]}, {\"id\": 2,\"from\": \"Veszprem\",\"to\": \"Gyor\",\"times\": [{ \"id\": 0, \"start\": \"2018.11.10. 15:46\", \"arrive\": \"2018.11.10. 16:50\" }],}, {\"id\": 1,\"from\": \"Gyor\",\"to\": \"Szombathely\",\"times\": [{ \"id\": 0, \"start\": \"2018.11.05. 13:10\", \"arrive\": \"2018.11.05. 14:50\" }],}]}";

public static void main(String[] args) {

    //RoutSystem routsystem = new RoutSystem();
    JSONObject object = new JSONObject(JSON_STRING);
    JSONArray routes = object.getJSONArray("routes");

    for (int y = 0; y < routes.length(); y++) {
        JSONObject route = routes.getJSONObject(y);
        int id = route.getInt("id");
        String from = route.getString("from");
        String to = route.getString("to");
        JSONArray time = route.getJSONArray("times");
        System.out.println(id);
        System.out.println(from);
        System.out.println(to);
        Routes rout = new Routes(id, from, to);
        for (int z = 0; z < time.length(); z++) {
            JSONObject valami = time.getJSONObject(z);
            int id1 = valami.getInt("id");
            String start = valami.getString("start");
            String arrive = valami.getString("arrive");
            System.out.println("time: " + id1 + " " + start + " " + arrive);
            rout.addTime(id1, start, arrive);
        }
        //rout.printRout();
        //routsystem.addRoute(rout);
    }

}
}

Routes.java Routes.java

import java.util.Vector;

public class Routes {

int id;
String from, to;
Vector<Times> times;

public Routes(int _id, String _from, String _to) {
    id = _id;
    from = _from;
    to= _to;
}

public void addTime(int id, String start, String arrive) {
    Times time = new Times();
    time.setid(id);
    time.setstart(start);
    time.setarrive(arrive);
    times.add(time);
}

public void printRout() {
    System.out.println(id);
    System.out.println(from);
    System.out.println(to);
    for (Times t : times) {
        System.out.println(t.getid() + " " + t.getstart() + " " + t.getarrive());
    }
}

}

Times.java Times.java

public class Times {
int id;
String start, arrive;


public Times() {
    id = 0;
    start = "";
    arrive = "";
}

public int getid() {
    return id;
}

public String getstart() {
    return start;
}

public String getarrive() {
    return arrive;
}

public void setid(int i) {
    id = i;
}

public void setstart(String s) {
    start = s;
}

public void setarrive(String a) {
    arrive = a;
}
}

The problem is you're not initializing your vector before adding objects into it. 问题是您没有在向其添加对象之前初始化向量。 That's why you get a nullpointer exception. 这就是为什么您会得到一个nullpointer异常的原因。 You should look at what a nullpointer is. 您应该看看什么是空指针。

what is a nullpointer exception and how do i fix it. 什么是nullpointer异常以及如何解决。

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

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