Sam L
Sam L

Reputation: 27

How to call a method on an array from the same class

I have a ParkingLot class with a getEmptySpaces() method that applies to ParkingLot objects, which are arrays of Car objects.

I want to call lot.getEmptySpaces(), but my IDE, Netbeans, throws a fit if I give it an array rather than a specific item. lot[1].getEmptySpaces() compiles fine, but crashes when it runs, as expected, since it's supposed to receive an array, not a null.

How do I call a method on an array defined by the same class?

// Main     
ParkingLot[] lot = new ParkingLot[10];
lot[1].getEmptySpaces(); // compiles but doesn't run
lot.getEmptySpaces(); // what i want to run but doesn't

// Car class
public class Car {
    
    private String color;
    private String licensePlate; // lp #

    public Car(String color, String licensePlate) {
        this.color = color;
        this.licensePlate = licensePlate;
    }

    /**
     * @return the color
     */
    public String getColor() {
        return color;
    }

    /**
     * @param color the color to set
     */
    public void setColor(String color) {
        this.color = color;
    }

    /**
     * @return the licensePlate
     */
    public String getLicensePlate() {
        return licensePlate;
    }

    /**
     * @param licensePlate the licensePlate to set
     */
    public void setLicensePlate(String licensePlate) {
        this.licensePlate = licensePlate;
    }

    @Override
    public String toString() {
        return "Car{" + "color=" + color + ", licensePlate=" + licensePlate + '}';
    }
}

// ParkingLot class
public class ParkingLot {
    
    private Car[] spaces; // lp=000000 color=none will represent an empty space
    private int currentIndex;

    /**
     * Creates a parkingLot object
     * 
     * @param size how many spaces are needed in the parking lot
     */
    public ParkingLot(int size) {
        // Array Example: String[] arr = new String[20];
        this.spaces = new Car[size];
        this.currentIndex = 0;
    }
    
    public int getEmptySpaces(){
        int emptySpaces = 0;
        for(int i = 0; i < spaces.length; i++){
            if (spaces[i] == null){
                emptySpaces++;
            }
        }
        return emptySpaces;
    }
    
    /**
     * Adds a car to the parking lot
     * 
     * @param car the car to be added to the parking lot
     */
    public void addCar(Car car){
       spaces[currentIndex] = car;
       currentIndex++;
    }
     
}

Upvotes: 0

Views: 259

Answers (2)

Sam L
Sam L

Reputation: 27

The problem I was having was that I created an array of ParkingLot objects. I only needed one ParkingLot, not 10.

My code should have been:

ParkingLot lot = new ParkingLot[10];

Instead of:

ParkingLot[] lot = new ParkingLot[10];

It was just a typo, like usual.

Upvotes: -2

rzwitserloot
rzwitserloot

Reputation: 103813

ParkingLot[] lot = new ParkingLot[10];

It feels like you imagine this creates a single parking lot with 10 spaces.

It doesn't.

It creates a plot of land upon which up to 10 parking lots can be placed, though none are there yet (a ParkingLot is an object, if you want one to exist, somebody, somewhere, must invoke new ParkingLot(), or no parking lot objects exist).

lot[1].getEmptySpaces()

This goes to the second lot (java is 0-indexed, so, lot[1] is the second lot), and asks it: Oi, how much free space is there? Given that you're yelling this at an empty space where a parking lot can be, but isn't, you get a NullPointerException.

lot.getEmptySpaces();

This makes no sense at all. How can you ask 10 parking lots at once? Even worse, how can you ask a bit of terrain reserved for 10 parking lots, but where no lots exist right now?

Relevant:

for (int i = 0; i <lots.size; i++) lots[i] = new ParkingLot();

Upvotes: 3

Related Questions