I am trying to add an object of type Car
to an Array of cars, I do not have a specific index within the array that I want the car to go into, I just want to add the car to the first empty and available index that doesn't have a car object already in there. Here is my code:
protected static final int MaxCars = 5;
protected Car[] cars = new Car[MaxCars];
public void addCar(Car c)
{
for(int i = 0; i < MaxCars; i++)
{
if (cars[i] == null)
{
cars[i] = c;
break;
}
}
incrementNumInTeam();
}
On the if statement inside the for loop I am getting the a NullPointerException
.. how can I overcome this?