代码报空指针异常-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

代码报空指针异常

2016-03-23 15:28:41 2680 3

代码如下:

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", "123@gmail.com");
    Person Jimmy = new Person("Jimmy", "456@gmail.com");
    Person Kobe = new Person("Kobe", "789@gmail.com");
    Person Fallon = new Person("Fallon", "123@hotmail.com");
    Person Adele = new Person("Adele", "456@hotmail.com");
    Person Rose = new Person("Rose", "789@hotmail.com");
    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 123@gmail.com.
Notify passenger Jimmy at 456@gmail.com.
Notify passenger Kobe at 789@gmail.com.
Exception in thread "main" java.lang.NullPointerException
at PassengerFlight.postpone(PassengerFlight.java:50)
at PassengerFlight.main(PassengerFlight.java:68)

希望能够得到大家的指正

取消 提交回答
全部回答(3)
  • zhuo阿蛮
    2019-07-17 19:11:18

    大兄弟,for (int i = 0; i <= this.getNumSold(); i++) { 这段的 i越界了
    i是从0开始的,i=3时时第四个数,而你只存了3个。
    i <= this.getNumSold() 改为 i < this.getNumSold()

    1 0
  • 小米3
    2019-07-17 19:11:18
    对象如果为空,然后使用它去调用方法则会报出空指针异常
    0 0
  • 蛮大人123
    2019-07-17 19:11:18

    我判断是这里写错了

    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()

    0 0
添加回答
相关问答

1

回答

什么是异常点检测(Outlier detection)?

2022-04-22 13:12:33 1149浏览量 回答数 1

1

回答

处理代码异常的格式是什么?

2022-04-03 22:15:47 565浏览量 回答数 1

1

回答

处理异常时,System.out.println代码表示什么意思呢?

2022-04-03 21:12:39 324浏览量 回答数 1

1

回答

异常处理可以用的代码有哪些?

2021-10-28 23:12:28 237浏览量 回答数 1

1

回答

oss断点下载偶尔报空指针异常

2020-05-21 10:29:04 897浏览量 回答数 1

1

回答

oss空指针

2016-03-30 16:48:44 2750浏览量 回答数 1

1

回答

空指针异常,在判断网络状态是否可用中

2016-03-20 11:05:34 1669浏览量 回答数 1

1

回答

spring事物管理 报空指针异常

2016-03-13 15:40:22 2104浏览量 回答数 1

1

回答

关于安卓viewpager空指针异常问题

2016-03-05 15:27:38 2137浏览量 回答数 1

1

回答

ONS C代码异常

2015-12-15 16:25:05 5413浏览量 回答数 1
+关注
蛮大人123
我说我不帅他们就打我,还说我虚伪
0
文章
7733
问答
问答排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载