0

I'm creating a parking garage simulator, and I have a nullPointerException error when calling displayState in line 47 of the manager class, and possibly further errors that I couldn't test for yet since I can't get past this one! Any advice would be appreciated.

public class Car {

private long timeIn;

public long getTime() {
    return timeIn;
}
        
public Car() {
    long timeIn = System.currentTimeMillis( );
}

...

public class Garage {

Car[] cars;

public Car[] getCar() {
    return cars;
}
public void setCars(Car auto, int index) {
    for (index=0; index<cars.length; index++) {
        auto = cars[index];
    }
}
//Checks if there is a car in each space
public boolean isEmpty(int k) {
    if (cars[k] == null) {
        return true;
    }
    else {
        return false;
    }
}
//Displays open and occupied spaces in the garage
public void displayState() {
    for (int i=0; i<cars.length; i++) {
        System.out.print(cars[i]);
        if (isEmpty(i)) {
            System.out.print('E');
        }
        else {
            System.out.print('C');
        }
    }
}
//Counts number of occupied parking spaces
public int park(Car auto) {
    int spacesFilled = 0;
    for (int i=0; i<cars.length; i++) {
        if (!isEmpty(i)) {
            auto = cars[i];
            spacesFilled += i;
        }
        
        else {
            spacesFilled++;
        }
        if (cars[i].equals(cars.length)) {
            spacesFilled = -1;
        }   
    }
    return spacesFilled;
    
}
//Removes a car from the lot and computes parking time
public double remove(int index) {
    double parkedTime = cars[index].getTime();
    cars[index] = null;
    return parkedTime;
}
//Finds where a specific car is parked based on randomly selected serial number
public int findBayOfCar(int carNumber) {
    int index = -1;
    for (int i=0; i<carNumber; i++) {
        index++;
        while (isEmpty(index)) {
            index++;
        }
    }
    return index;
}
public Garage(int capacity) {
    cars = new Car[capacity];
}

}

...

import java.util.Random;

import javax.swing.JOptionPane;

public class Manager {

private Garage garage;
private final double FEE_PER_HOUR = 1.50;
private double feeTotal = 0;
int manyCars;

//Randomly selects a space for a new car to park
public void parkACar() {
    double index = garage.park(new Car());
    if (index != -1) {
        JOptionPane.showMessageDialog(null, "A car is arriving at bay #3.", "Parking Management", JOptionPane.OK_OPTION);
        manyCars++;
        garage.displayState();
    }
    else {
        JOptionPane.showMessageDialog(null, "The parking garage is full\nThe parking process terminates.", "Parking Management", JOptionPane.WARNING_MESSAGE);
        System.out.printf("Total parking fee collected is %f", feeTotal);
        System.exit(0);
    }
}
//Randomly selects a car to leave the garage
public void chooseACarToLeave() {
    if (garage.isEmpty(manyCars)) {
        JOptionPane.showMessageDialog(null, "The parking garage is empty\nThe parking process terminates.", "Parking Management", JOptionPane.WARNING_MESSAGE);
        System.out.println("After 15 parking operations the process is terminated.");
        System.exit(0);
    }
    else {
        Car car = new Car();
        Random rand = new Random();
        int bay = rand.nextInt(manyCars);
        int index = garage.findBayOfCar(bay);
        garage.remove(index);
        feeTotal = car.getTime() * FEE_PER_HOUR;
        JOptionPane.showMessageDialog(null, String.format("The car from bay number %d is leaving the garage.\nParking fee paid: %f", bay, feeTotal), "Parking Management", JOptionPane.OK_OPTION);
        garage.displayState();
        manyCars--;
    }
}
//For loop to choose either parkACar or chooseACarToLeave
public void processParking(int limit) {
    JOptionPane.showMessageDialog(null, "Welcome to the garage parking simulation!\nSee the initial state of the garage on the console.", "Parking Management", JOptionPane.OK_OPTION);
    garage.displayState();  
    Random rand = new Random();
    double probability = rand.nextDouble();
    for (int i=0; i<manyCars; i++) {
        if (probability <= .5) {
            parkACar();
        }
        else {
            chooseACarToLeave();
        }
    }
}       
public Manager(int many, Garage gar) {
    manyCars = many;
    gar = garage;
}

}

...

import java.util.Random;

public class Application {

public static void main(String[] args) {
    int garageCapacity = 15;
    int limit = 15;
    Garage garage = new Garage(garageCapacity);
    int count = 0;
    
    //visit all the parking bays; at each bay a new Car object is
    //added to the bay with probability 0.5 and counts number of new cars
    for (int i=0; i<garageCapacity; i++) {
        Random rand = new Random();
        double probability = rand.nextDouble();
        if (probability <= .5) {
            Car car = new Car();
            ;
            count++;
        }
    }
    Manager manage = new Manager(count, garage);
    manage.processParking(limit);
}

}

rakers
  • 41
  • 6
  • For future questions: please read [mcve] and focus on providing only enough of the relevant code. Also note that things such a stack trace should be included. – GhostCat Dec 13 '20 at 20:25
  • Here is an actual answer: set garage = gar in your constructor, (for your manager constructor) not the other way around. You are setting the value of gar which does nothing. – bdehmer Dec 13 '20 at 20:27
  • 1
    @bdehmer thank you this is giving me output now – rakers Dec 14 '20 at 16:22

0 Answers0