程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> java-NullPointerException報錯是怎麼一回事?

java-NullPointerException報錯是怎麼一回事?

編輯:編程綜合問答
NullPointerException報錯是怎麼一回事?
public class PassengerFlight extends Flight {

private int numSold;
private int seat;
private Person[] passengers;

public PassengerFlight(String num, String city1, String city2, Time departure, int length, int seat) {
    super(num, city1, city2, departure, length);
    this.seat = seat;
    this.numSold = 0;
    this.passengers = new Person[seat];
}

public int getNumSold() {
    return this.numSold;
}

public int getSeats() {
    return this.seat;
}

public int getNumAvailable() {
    return this.getSeats() - this.getNumSold();
}

public boolean sellTicket(Person buyer) {
    if (this.getNumAvailable() > 0) {
        passengers[numSold] = buyer;
        this.numSold++;
        System.out.println("Ticket sold to " + buyer.getName() + ".");
        return true;
    } else
        System.out.println("Cannot sell more tickets - flight " + this.getFlightnum() + " is full.");
    return false;

}

public String toString() {
    return "Passenger flight " + super.toString() + " " + this.getNumAvailable() + " seats out of " + this.seat
            + " are available.";

}

public void postpone(int minutes) {
    System.out.println("");
    super.postpone(minutes);
    System.out.println("Flight " + this.getFlightnum() + " is postponed till " + this.getDepartureTime());
    for (int i = 0; i <= this.getNumSold(); i++) {
        System.out.println("Notify passenger " + passengers[i].getName() + " at " + passengers[i].getEmail() + ".");
    }
}

public static void main(String[] args) {
    Time departureTime = new Time("17:15");
    PassengerFlight flight1 = new PassengerFlight("3485", "BOSTON", "SEATTLE", departureTime, 240, 5);
    System.out.println(flight1);
    Person Jieliang = new Person("Jie", "[email protected]");
    Person Jimmy = new Person("Jimmy", "[email protected]");
    Person Kobe = new Person("Kobe", "[email protected]");
    Person Fallon = new Person("Fallon", "[email protected]");
    Person Adele = new Person("Adele", "[email protected]");
    Person Rose = new Person("Rose", "[email protected]");
    flight1.sellTicket(Jie);
    flight1.sellTicket(Jimmy);
    flight1.sellTicket(Kobe);
    System.out.println(flight1);
    flight1.postpone(35);
    System.out.println(flight1);
    flight1.sellTicket(Fallon);
    flight1.sellTicket(Adele);
    flight1.sellTicket(Rose);
    System.out.println(flight1);
}}

以下是報錯的顯示
Passenger flight 17:15 3485 BOSTON--SEATTLE 240 min. 5 seats out of 5 are avail
able.
Ticket sold to Jieliang.
Ticket sold to Jimmy.
Ticket sold to Kobe.
Passenger flight 17:15 3485 BOSTON--SEATTLE 240 min. 2 seats out of 5 are avail
able.

Flight 3485 is postponed till 17:50
Notify passenger Jie at [email protected].
Notify passenger Jimmy at [email protected].
Notify passenger Kobe at [email protected].
Exception in thread "main" java.lang.NullPointerException
at PassengerFlight.postpone(PassengerFlight.java:50)
at PassengerFlight.main(PassengerFlight.java:68)

希望能夠得到大家的指正

最佳回答:


我判斷是這裡寫錯了
for (int i = 0; i <= this.getNumSold(); i++) {
System.out.println("Notify passenger " + passengers[i].getName() + " at " + passengers[i].getEmail() + ".");
}
你把i <= this.getNumSold() 改為 i < this.getNumSold() 試一下

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved