0
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);
    }
}

我一直在尝试创建一个程序来创建和签发停车罚单,并且在编译时遇到了问题,但是当它运行时,它在线程“main”java.lang.NullPointerException 中给出错误消息异常。我是一个相当新的程序员,这是我第一次遇到这个问题,所以我还没有完全理解它,似乎无法修复它。我曾尝试在线阅读其他内容,但只是不明白我希望对我的问题进行简单的解释。

4

1 回答 1

5

NPE 的发生是因为这两行:

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

在你的构造函数中PoliceOfficer,你不对ParkingTicket实例做任何事情pt

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

字段ParkingMeter pmParkingTicket pt保持为空,因为您尚未初始化它们。

然后你尝试打印对象:System.out.println(po);this 的作用是调用toString()on po,它等价于:

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

现在因为你的 toString()

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

使用pt,它会创建一个 NullPointerException,因为pt它是 null。

由于您已经将一个ParkingTicket实例传递给 for 的构造函数,因此请PoliceOfficer使用该实例来分配其成员变量pt

于 2013-02-24T00:40:37.807 回答