如何将内部JSONArray数据放入向量中?只有当我想把数据放入向量时,程序才会崩溃。当我只是打印数据时,一切都很好。为什么?如何解决此问题?当我用矢量运行程序时:
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)
当我运行whitut-vecor时(只需将良好的输出打印到控制台(:
0
布达佩斯
索尔诺克
时间:0 2018.10.21。11:20 2018.10.21。13:25
时间:2018年10月21日。13:20 2018.10.21。15:25
1
Veszprem
布达佩斯
时间:0 2018.10.30。09:35 2018.10.30。11:02
2
Veszprem
Gyor
时间:0 2018.11.10。15:46 2018.11.10。16:50
1
Gyor
Szombathly
时间:0 2018.11.05。13:10 2018.11.05。下午14:50
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
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
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;
}
}
问题是在向向量中添加对象之前没有初始化向量。这就是为什么您会得到一个nullpointer异常。你应该看看什么是空指针。
什么是nullpointer异常,我该如何修复它。