0

My program currently takes in data on cars, like weight, engine size and color, it then creates an object car and sets those values inside the object.

The object is then added to an Arraylist of car which is in a separate class. When trying to display the properties of say the first car by doing Arraylist.get(0).color it says my Arraylist is null.

I've looked into it and it seems like I'm re-initializing the Arraylist but I don't see how.

This is the code that sets the object car as well as the code for the car object and the constructor for the class:

public class AddCar {

    public static MainMenu menu = new MainMenu();
    public Car car;
    CarList carList;

    //constructor for the AddCar
    public AddCar(){carList = new CarList();}

    public class Car{

        int weight;
        String engine;
        String color;

        public Car(){}

        public Car(weight, engine, color)
        {
            wt = weight;
            eng = engine;
            col = color;

        }


    }

    public void addCar(){

        int weight = 3000;
        String engine = 6L
        String color = blue;

        car = new Car(weight, engine, color);

        carList.addCar(car);

        menu.user_input();
    }

public void displayCar()
{   
    custList.dispCar();
}

The method addCar is from a separate class file which just adds the car to the end of an Arraylist, here is that code:

public class CarList {

    List<Car> car_list;

    public CarList()
    {
        car_list = new ArrayList<Car>(10);
    }
    //method to add car to arraylist
    public void addCar(Car car)
    {

        car_list.add(car);
        //this print statement works fine
        System.out.println(car_list.get(0).color);

    }
    //method to display color property of first car
    public void dispCar()
    {
        //this print statement comes back with null pointer exception
        System.out.println(car_list.get(0).color);
    }

This issue occurs when you use the method dispCar from the above class, it throws a null pointer exception.

And then this is the MainMenu where you choose the option for what to do:

public class MainMenu {

    private static Scanner keyboard1 = new Scanner(System.in);
    private AddCar add;

    public MainMenu()
    {
        add = new AddCar();
    }

    public void user_input()
    {
        System.out.println("Pick an Option:");
        System.out.println("1. Add a car");
        System.out.println("2. Remove a car");

        System.out.println("Please enter the option #:");
        int option = keyboard1.nextInt();

        switch (option){
        case 1: add.addCar();
        case 2: add.displayCar();
        }   
    }   
}
9
  • Your code misses the method displayCar of the class AddCar, please add it to your question. Commented Jan 28, 2017 at 15:15
  • have you added a car to the AddCar(your case1) before displayCar? Commented Jan 28, 2017 at 15:15
  • @hotzst - I just added the method, I had it in my original code just missed it here, thanks! Commented Jan 28, 2017 at 15:49
  • @nullpointer - yeah, sorry I should have said that it takes user input and you do 1 and add a car then 2 to display the car info. Commented Jan 28, 2017 at 15:50
  • The type declared for the first parameter of the CarList.addCar method is Customer when it should be Car Commented Jan 28, 2017 at 15:57

1 Answer 1

2

You got lost in dependency handling I'm afraid.

Your top-level class would be MainMenu. I belive you'd like to have just one at all times, and just call it's method when you need to. That's fine. It creates AddCar instance.

And this is where bad things happen - AddCar has this line:

public static MainMenu menu = new MainMenu();

That means, that it will have it's own MainMenu instance, separate from what you used to create any AddCar instance. So when you call menu.user_input() in addCar method, you are calling it on a separate instance of MainMenu, one with different AddCar that holds a different CarList which is still empty.

As to how to fix it - have MainMenu be that main class, in order to keep going make a loop instead of calling back to MainMenu from AddCar method, and remove any mention of MainMenu from it. Oh, and add break statements in your switch, or else it's behaviour might suprise you.

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

3 Comments

I love you. That fixed the issue, it's been driving me crazy but I understand what I was doing now too.
@Heights feel free to accept the answer if it fixed your issue.
Sorry, just did it!

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.