嘿伙计们,我正在计算机科学课上完成最后一个项目。它将是一个非常简单的航空系统实时模拟。我刚开始,所以大多数仍然是占位符代码,它仍然没有注释和可怕所以不要太苛刻,但我得到一个非常奇怪的空指针异常错误。我已经在输出中添加了调试行,所以你可以看到它发生了。
您可以立即获取源代码here。
基本上,类fileManager()以递归方式循环遍历文件夹并查找所有.inis并将它们放在链接列表中。然后,renderingArea()的构造函数根据.inis的#和它们的默认值填充city []。当我尝试将文件plane.ini的位置作为InputStream传递给类plane()的构造函数时,我得到一个空指针异常错误。有人可以帮忙吗?
class renderingArea extends JPanel {
public fileManager files;
public city[] cities;
public renderingArea(){
// ... other code
for( loop through all files in fileManager ){
File current = files.ini.get(i);
if(current.getName().contains("city")){
try{
InputStream cityStream = files.getResource(current.getName());
InputStream planeStream = files.getResource("plane.ini");
cities[index] = new city( cityStream, planeStream);
cityStream.close();
planeStream.close();
index++;
} catch (Exception e) {
e.printStackTrace();
}
}
}
for( city current : cities){
current.setCities(cities);
}
}
// ============== Class City ========================
public class city {
private final String[] keys = {"x","y","name","population","planes_in_hanger"};
public float x;
public float y;
public int population;
public String name;
private Queue<passenger>[] waiting_passengers; // queue[] parallel to cities.
private Queue<plane> planes_in_hanger; // a queue is a first in first out ADT. Standard ops apply
private city[] cities;
private IniReader ini;
public city(InputStream inStream, InputStream inStreamPlane) throws IOException, FileNotFoundException {
System.out.println("city: " + inStream.toString());
System.out.println("plane: " + inStreamPlane.toString());
ini = new IniReader();
ini.load(inStream);
// .... Other Code
if(ini.properties.containsKey("planes_in_hanger")){
try{
for( int i = 0; i < Integer.parseInt(ini.properties.getProperty("planes_in_hanger")); i++){
System.out.println("iter: "+i);
System.out.println("plane: "+inStreamPlane.toString());
planes_in_hanger.enqueue(new plane(inStreamPlane));
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
}
public class plane {
private final String[] keys = {"x","y","max_velocity","passengers"};
public float x;
public float y;
public float max_velocity;
private float x_velocity;
private float y_velocity;
public city destination;
private passenger[] passengers;
public int max_passengers;
private IniReader ini;
//====================== CLASS PLANE ======================
public plane(InputStream inStream) throws IOException, FileNotFoundException{
ini = new IniReader();
ini.load(inStream);
//rest doesn't matter
}
输出中:
//调试内容,切换到异常
java.lang.NullPointerException at city.(city.java:72) at renderingArea.(flight_optimizer.java:70) at flight_optimizer_GUI.(flight_optimizer.java:103) at flight_optimizer.main(flight_optimizer.java:37) Exception in thread "main" java.lang.NullPointerException at renderingArea.(flight_optimizer.java:80) at flight_optimizer_GUI.(flight_optimizer.java:103) at flight_optimizer.main(flight_optimizer.java:37)
答案 0 :(得分:2)
您似乎没有在任何地方初始化新的planes_in_hanger,但您将其声明在顶部。这可能是你的问题?
private Queue<plane> planes_in_hanger = new Queue<plane>();
或者你可以用该方法初始化它。