1

I am getting the java.lang.NullPointerException on the cars.add(car); line and I am really unsure how to solve it, I have read some of the other threads relating to this error, but the answer still seems to have eluded me.

public class RentalCompany {

    private ArrayList<Car> cars;
    private String description;
    private int carDownPayment;
    private int rate;
    private int carNumber;

    public void addCar(String descriptionOfTheCar, int downPayment,
        int dailyRate) {
        Car car = new Car(descriptionOfTheCar, downPayment, dailyRate);
        cars.add(car);
    }
}

10 Answers 10

7

Because you haven't initialized the private ArrayList<Car> cars field. Do it either lazily, in a constructor or in the method itself.

private ArrayList<Car> cars = new ArrayList<Car>();

OR

public RentalCompany() {
    this.cars = new ArrayList<Car>();
}

OR

public void addCar(String descriptionOfTheCar, int downPayment,
    int dailyRate) {
    this.cars = new ArrayList<Car>();
    Car car = new Car(descriptionOfTheCar, downPayment, dailyRate);
    cars.add(car);
}

I personally prefer the constructor approach.

2
  • Oh, and I know that a sentence can't begin with 'Because'
    – adarshr
    Jan 13, 2012 at 13:15
  • Hmmm... Option 3 overrides this.cars with a new ArrayList every time the addCar function is called. The content of this.cars will be deleted at each addCar invocation and cars will never contain more than one element. Jan 13, 2012 at 15:07
5

Or you can use:

    public void addCar(String descriptionOfTheCar, int downPayment, int dailyRate)
        {
            if (cars == null)
                cars = new ArrayList<>();

            Car car = new Car(descriptionOfTheCar, downPayment, dailyRate);
            cars.add(car);
        }

assuming its one thread access

0
3

You didn't initiate cars.

private ArrayList<Car> cars = new ArrayList<Car>();
2

Init list:

private ArrayList<Car> cars = new ArrayList<Car>();
0

cars has not been initialised.

Try adding the following to your constructor:

cars = new ArrayList<>();

before you add cars to it.

0

your cars was not initialized, which is null. and you tried to add Elements to this null arraylist.

you could initialize it before using.

Like

cars=new ArrayList<Car>();
Car car = new Car(descriptionOfTheCar, downPayment, dailyRate);
cars.add(car);
0

Without seeing any details of what is calling addCar() I can't say for certain, but the two most obvious possibilities are that the String has not been initialised or that the ArrayList has not been initialised.

0

I assume much of your code has been omitted, but from this sample it seems that you haven't initialized your ArrayList.

0

looks like you didn't initialize your cars ArrayList.

0

Initially cars == null, so initialize it as follow:

 private ArrayList<Car> cars = new ArrayList<Car>();

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.