1
嘿,我正在研究計算機科學課的最後一個項目。它將成爲航空公司系統的一個非常簡單的實時模擬。我剛開始,所以大部分仍然是佔位符代碼,它仍然沒有註釋和可怕,所以不要太苛刻,但我得到一個非常奇怪的空指針異常錯誤。我已將調試行添加到輸出中,以便您可以看到它發生。InputStreams奇怪的空指針異常
你可以像現在這樣獲取源代碼here。
基本上,類fileManager()遞歸循環遍歷文件夾並找到所有.inis並將它們放置在鏈接列表中。 renderingArea()的構造函數然後根據.inis的#和它們的默認值填充城市[]。當我嘗試將文件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)
你應該在你的問題中發佈相關代碼......大多數人不想下載一個zip文件。 – ColinD 2011-04-14 13:47:29
只是一個提示:沒有人會下載,解壓縮,編譯和運行您的整個自我承認可怕的代碼,只是爲了能夠重新創建錯誤條件。如果你想讓人們來幫助你,那麼讓他們先跳出來是一個不好的主意。好得多:顯示堆棧跟蹤和發生異常的代碼行。 NPE通常非常容易以這種方式診斷。 – 2011-04-14 13:48:54
嘿,代碼是非常相互關聯的,我發佈的任何代碼幾乎與實際的源代碼一樣長,並且不清楚發生了什麼。我可以發佈它,如果你想要它。 – Ignoreme 2011-04-14 13:58:00