简体   繁体   中英

I am getting “Exception in thread ”main" java.lang.NullPointerException even though no values are set to null

public class ParkedCar {
    private String make;
    private String model;
    private String color;
    private String licenseNum;
    public ParkedCar(String make, String model, String color, String licenseNum) {
        this.make = make;
        this.model = model;
        this.color = color;
        this.licenseNum = licenseNum;
    }
    public void setMake(String ma) {
        make = ma;
    }
    public void setModel(String mo) {
        model = mo;
    }
    public void setColor(String c) {
        color = c;
    }
    public void setLicenseNum(String ln) {
        licenseNum = ln;
    }
    public String getMake() {
        return make;
    }
    public String getModel() {
        return model;
    }
    public String getColor() {
        return color;
    }
    public String getLicenseNum() {
        return licenseNum;
    }
}

public class ParkingMeter {
    private ParkedCar parkedcar;
    private int timePurchased;
    private int timeParked;
    public ParkingMeter(ParkedCar parkedcar, int timePurchased, int timeParked) {
        this.parkedcar = parkedcar;
        this.timePurchased = timePurchased;
        this.timeParked = timeParked;
    }
    /*public ParkingMeter (ParkedCar parkedcar) {
        this.parkedcar = null;
    }*/
    public void setTimePurchased(int timePurchased) {
        this.timePurchased = timePurchased;
    }
    public int getTimePurchased() {
        return timePurchased;
    }
    public void setTimeParked(int timeParked) {
        this.timeParked = timeParked;
    }
    public int getTimeParked() {
        return timeParked;
    }
    public int TimeExpired() {
        if (timeParked > timePurchased) 
            return timeParked - timePurchased;
        else    
            return 0;
    }
    public String toString() {
        return "Make: " + parkedcar.getMake() + "\nModel: " + parkedcar.getModel() + "\nColor: " + parkedcar.getColor() + "\nLicense Number: " + parkedcar.getLicenseNum();
    }
}

public class ParkingTicket {
    private ParkingMeter parkingmeter;
    public ParkingTicket(ParkingMeter parkingmeter) {
        this.parkingmeter = parkingmeter;
    }
    public int TicketCost() {
        if (parkingmeter.getTimeParked() > parkingmeter.getTimePurchased()) {
            if (parkingmeter.getTimeParked() <= 60)
                return 25;
            else 
                return 25 + (10*(parkingmeter.TimeExpired())/60);
        }
        else 
            return 0;
    }                           
}

public class PoliceOfficer {
    private String OfficerName;
    private int OfficerNum;
    private ParkingMeter pm;
    private ParkingTicket pt;
    public PoliceOfficer(ParkingTicket pt, String OfficerName, int OfficerNum) {
        this.OfficerName = OfficerName;
        this.OfficerNum = OfficerNum;   
    }
    public void setOfficerName(String OfficerName) {
        this.OfficerName = OfficerName;
    }
    public void setOfficerNum(int OfficerNum) {
        this.OfficerNum = OfficerNum;
    }
    public String getOfficerName() {
        return OfficerName;
    }
    public int getOfficerNum() {
        return OfficerNum;
    }
    public boolean isExpired() {
        if (pm.getTimeParked() > pm.getTimePurchased())
            return true;
        else
            return false;
    }
    public String toString() {
        return "Officer Name: " + OfficerName + "\nOfficer Number: " + OfficerNum + "\n" + "\nFine: " + pt.TicketCost();    
    }
}

public class ParkingTicketDemo {
    public static void main(String[] args) {
        ParkedCar pc = new ParkedCar("Toyota", "Camry", "Blue", "BXZ 152");
        System.out.println(pc);
        ParkingMeter pm = new ParkingMeter(pc, 60, 120);
        ParkingTicket pt = new ParkingTicket(pm);
        PoliceOfficer po = new PoliceOfficer(pt, "Roger", 337);
        System.out.println(po);
    }
}

I have been trying to create a program to create and issue a parking ticket and have run into the problem where it compiles, but when it runs it gives out the error message Exception in thread "main" java.lang.NullPointerException. I am a fairly new programmer and this is the first time I have encountered the problem so I have yet fully understand it and cannot seem to fix it. i have tried reading other things online, but just do not understand I would love a simple explaination to my problem.

The NPE happens because of these two lines:

PoliceOfficer po = new PoliceOfficer(pt, "Roger", 337);
System.out.println(po);

In your constructor for PoliceOfficer , you don't do anything with the ParkingTicket instance pt .

public PoliceOfficer(ParkingTicket pt /* not set anywhere */, String OfficerName, int OfficerNum) {
    this.OfficerName = OfficerName;
    this.OfficerNum = OfficerNum;   
}

The fields ParkingMeter pm and ParkingTicket pt remain null since you haven't initialized them.

Then you try to print the object: System.out.println(po); What this does is call toString() on po , it is equivalent to this:

System.out.println(po.toString());

Now because your toString()

public String toString() {
    return "Officer Name: " + OfficerName + "\nOfficer Number: " + OfficerNum + "\n" + "\nFine: " + pt.TicketCost();    
}

uses the pt , it creates a NullPointerException, since pt is null.

Since you are already passing a ParkingTicket instance into the constructor for PoliceOfficer , use that instance to assign its member variable pt .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM