2011-04-14 75 views
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)
+8

你应该在你的问题中发布相关代码......大多数人不想下载一个zip文件。 – ColinD 2011-04-14 13:47:29

+12

只是一个提示:没有人会下载,解压缩,编译和运行您的整个自我承认可怕的代码,只是为了能够重新创建错误条件。如果你想让人们来帮助你,那么让他们先跳出来是一个不好的主意。好得多:显示堆栈跟踪和发生异常的代码行。 NPE通常非常容易以这种方式诊断。 – 2011-04-14 13:48:54

+0

嘿,代码是非常相互关联的,我发布的任何代码几乎与实际的源代码一样长,并且不清楚发生了什么。我可以发布它,如果你想要它。 – Ignoreme 2011-04-14 13:58:00

回答

2

似乎你不被任何地方初始化一个新的planes_in_hanger但你在顶部声明。这可能是你的问题?

private Queue<plane> planes_in_hanger = new Queue<plane>(); 

或者你可以在该方法中初始化它。