Hi I'm creating a 2D array of objects in java and want to access one of the functions that simply returns the boolean value of the variable but for some reason, its throwing an error. The code looks so simple but I can't figure out what I am doing wrong. My guess is something to do with my implementation of the array, but I think I did it right. My goal is to create individual instances of the class for each value in the 100x100 grid.
Here is the code and error I am getting
public class Main {
public static void main(String[] args) {
ParkingLot parkingLot = new ParkingLot("Rushi's Parking lot", 100, 100);
Vehicle car1 = new Car("License");
parkingLot.registerVehicle();
}
}
public class ParkingLot {
String name;
ParkingSpot[][] parkingGrid;
public ParkingLot(String name, int xSize, int ySize){
this.name = name;
this.parkingGrid = new ParkingSpot[xSize][ySize];
}
public void registerVehicle() {
System.out.println(parkingGrid[0][0].isAvailable());
}
public void getNextSpot(Vehicle vehicle){
}
}
public class ParkingSpot {
private boolean available = true;
private String license;
private int time;
public boolean isAvailable() {
return available;
}
public void reserveSpot(String license){
this.license = license;
this.available = false;
}
}
Error:
Exception in thread "main" java.lang.NullPointerException
at ParkingLot.registerVehicle(ParkingLot.java:14)
at Main.main(Main.java:7)