It's because in this line:
Code:
private Car[] database = new Car[3];
You are creating a new array of Car, but when you do this:
Code:
for (int i=0; i<database.length; i++) {
database[i].formatCar();
}
The code in
red returns a null Car object because the Car instance has not been created yet for that position in the array. Then when it attempts to run the formatCar method, it returns a NullPointerException because you're trying to do the formatCar method on a null object. Before doing the line in red, you should do:
Code:
database[i] = new Car();
However, looking at your code, you should replace the array and use an ArrayList object instead like so:
Code:
List<Car> database = new ArrayList<Car>(3);
Then you won't have to create your own addCar method since an ArrayList already has that method implemented.
-Shane