InputStreams 出现奇怪的空指针异常

发布于 2024-11-01 11:07:54 字数 3546 浏览 3 评论 0原文

大家好,我正在做计算机科学课的期末项目。这将是一个非常简单的航空公司系统实时模拟。我刚刚开始,所以其中大部分仍然是占位符代码,并且它仍然没有注释并且很糟糕,所以不要太严厉,但是我收到了一个非常奇怪的空指针异常错误。我已将调试行添加到输出中,以便您可以看到它的发生。

您现在可以在此处获取源代码。

基本上,类 fileManager() 递归地循环遍历文件夹并查找所有 .ini 并将它们放入链接列表中。然后,renderingArea() 的构造函数根据 .ini 的 # 及其默认值填充 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)

Hey guys, I am working on a final project in my computer science class. Its a going to be a very simple real time simulation of an airline system. I just started, so most of this is still place holder code, and it is still uncommented and terrible so don't be too harsh yet, but I am getting a very strange null pointer exception error. I've added debug lines to the output so you can see it happening.

you can grab the source code as is right now here.

Basically, the class fileManager() recursively loops through the folders and finds all the .inis and places them in a linked list. The constructor of renderingArea() then populates a city[] based on the # of .inis and the default values they have. I'm getting a null pointer exception error when I try to pass the location of the file plane.ini to the constructor for the class plane() as an InputStream. Can anyone help?

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
}

ouput:

//debug stuff, cutting to exception

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落墨 2024-11-08 11:07:54

您似乎没有在任何地方初始化新的planes_in_hanger,但您在顶部声明了它。这可能是你的问题?

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

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

You don't seem to be initializing a new planes_in_hanger anywhere but you declare it at the top. This could possibly be your issue?

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

Or you can just initialize it in that method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文