1

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)
6
  • 8
    You should probably post the relevant code in your question... most people aren't going to want to download a zip file. Commented Apr 14, 2011 at 13:47
  • 12
    Just a hint: nobody is going to download, unzip, compile and run your entire self-admittedly horrible code just to be able to recreate the error condition. If you want people to help you, making them jump through hoops first is a bad idea. Much better: show stacktrace and the line of code where the exception occurs. NPEs are usually very, very easy to diagnose that way. Commented Apr 14, 2011 at 13:48
  • Hey, the code is very interconnected, any code I posted would be almost as long as the actual source code and It would not be as clear what is happeneing. I can post it if you want it. Commented Apr 14, 2011 at 13:58
  • 4
    A good policy when posting questions here (or anywhere) is to remove bits of code until you have the smallest possible amount of code that demonstrates your bug. Interestingly, this often shows you what you did wrong before you even post. Commented Apr 14, 2011 at 14:03
  • planes_in_hanger.enqueue(new plane(inStreamPlane)); Commented Apr 14, 2011 at 14:22

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.