Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java InputStreams出现奇怪的空指针异常_Java_Nullpointerexception_Inputstream - Fatal编程技术网

Java InputStreams出现奇怪的空指针异常

Java InputStreams出现奇怪的空指针异常,java,nullpointerexception,inputstream,Java,Nullpointerexception,Inputstream,嘿,伙计们,我在计算机科学课上做最后一个项目。这将是一个非常简单的航空系统实时模拟。我刚刚开始,所以大部分代码仍然是占位符代码,它仍然是未注释的和可怕的,所以不要太苛刻,但我得到了一个非常奇怪的空指针异常错误。我已经在输出中添加了调试行,以便您可以看到它的发生 您现在就可以获取源代码 基本上,类fileManager()递归地遍历文件夹,找到所有的.ini并将它们放在一个链表中。renderingArea()的构造函数然后根据#of.ini及其默认值填充城市[]。当我试图将文件plane.ini

嘿,伙计们,我在计算机科学课上做最后一个项目。这将是一个非常简单的航空系统实时模拟。我刚刚开始,所以大部分代码仍然是占位符代码,它仍然是未注释的和可怕的,所以不要太苛刻,但我得到了一个非常奇怪的空指针异常错误。我已经在输出中添加了调试行,以便您可以看到它的发生

您现在就可以获取源代码

基本上,类fileManager()递归地遍历文件夹,找到所有的.ini并将它们放在一个链表中。renderingArea()的构造函数然后根据#of.ini及其默认值填充城市[]。当我试图将文件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
}
class renderingArea扩展了JPanel{
公共文件管理器文件;
公共城市[]个城市;
公共渲染区域(){
//…其他代码
for(在fileManager中循环浏览所有文件){
当前文件=files.ini.get(i);
如果(current.getName()包含(“城市”)){
试一试{
InputStream cityStream=files.getResource(current.getName());
InputStream planeStream=files.getResource(“plane.ini”);
城市[指数]=新城(城市流、平面流);
cityStream.close();
planetstream.close();
索引++;
}捕获(例外e){
e、 printStackTrace();
}
}
}
用于(当前城市:城市){
当前城市(城市);
}       
}
//===================一流城市========================
公营城市{
私有最终字符串[]键={“x”、“y”、“name”、“population”、“planes\u in\u hanger”};
公共浮动x;
公众浮躁;
公共人口;
公共字符串名称;
私人队列[]等待乘客;//队列[]与城市平行。
专用队列平面\u in\u hanger;//队列是先进先出ADT。标准操作适用
私人城市[]城市;
私人图书馆;
公共城市(InputStream inStream、InputStreamPlane)引发IOException、FileNotFoundException{
System.out.println(“城市:+inStream.toString());
System.out.println(“平面:+流内平面.toString());
ini=新的IniReader();
初始载荷(流内);
//……其他代码
if(ini.properties.containsKey(“悬挂器中的平面”)){
试一试{
对于(inti=0;i
输出:

//调试东西,切到异常

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) java.lang.NullPointerException 在城市。(city.java:72) 在渲染区。(flight_optimizer.java:70) 在flight\u optimizer\u GUI上。(flight\u optimizer.java:103) 在flight\u optimizer.main(flight\u optimizer.java:37) 线程“main”java.lang.NullPointerException中出现异常 在渲染区。(flight_optimizer.java:80) 在flight\u optimizer\u GUI上。(flight\u optimizer.java:103)
在flight\u optimizer.main(flight\u optimizer.java:37)中,您似乎没有在任何地方初始化新的planes\u,而是在顶部声明它。这可能是您的问题

private Queue<plane> planes_in_hanger = new Queue<plane>();
private Queue planes\u in\u hanger=new Queue();

或者你可以用那种方法初始化它。

你可能应该在你的问题中发布相关的代码……大多数人都不想下载zip文件。只是一个提示:没有人会下载、解压、编译并运行你自己公认的可怕代码,只是为了能够重新创建错误条件。如果你想让人们下载的话帮助你,让他们先跳转是个坏主意。更好的办法是:显示stacktrace和发生异常的代码行。NPE通常非常非常容易通过这种方式进行诊断。嘿,代码是非常相互关联的,我发布的任何代码都几乎与实际源代码一样长,并且不清楚发生了什么。如果你愿意,我可以发布。在这里(或任何地方)发布问题时,一个好的策略是删除一些代码,直到你有尽可能少的代码来展示你的bug。有趣的是,这经常会显示你在发布.planes_in_hanger.enqueue(新平面(流内平面))之前犯了什么错误;