微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

InputStreams产生奇怪的Null指针异常

如何解决InputStreams产生奇怪的Null指针异常

| 大家好,我正在计算机科学课的最后一个项目上工作。这将是对航空公司系统的非常简单的实时仿真。我刚开始,所以其中大多数仍然是占位符代码,并且仍然没有注释和可怕,因此还不算太苛刻,但是我遇到了一个非常奇怪的空指针异常错误。我已经在输出添加了调试行,因此您可以看到它的发生。 您可以在此处立即获取代码。 基本上,类fileManager()递归地遍历文件夹并找到所有.inis并将它们放置在链接列表中。然后,renderArea()的构造函数根据.inis的数量及其认值填充city []。当我尝试将文件plane.ini的位置传递给类plane()的构造函数作为InputStream时,出现空指针异常错误。有人可以帮忙吗?
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\",\"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     在城市。(city.java:72)     在renderingArea(flight_optimizer.java:70)     在flight_optimizer_GUI。(flight_optimizer.java:103)     在flight_optimizer.main(flight_optimizer.java:37) 线程\“ main \”中的异常java.lang.NullPointerException     在renderingArea(flight_optimizer.java:80)     在flight_optimizer_GUI。(flight_optimizer.java:103)     在flight_optimizer.main(flight_optimizer.java:37)     

解决方法

您似乎没有在任何地方初始化新的planes_in_hanger,但在顶部声明了它。这可能是您的问题?
private Queue<plane> planes_in_hanger = new Queue<plane>();
或者,您可以仅使用该方法对其进行初始化。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。