使用对象数组的空指针异常

时间:2013-07-15 08:29:59

标签: arrays exception object pointers null

我打算建立航空公司系统。我使用initSeats初始化了数组,但它仍然会抛出NPE错误。当我从bookMenu调用seatChecker()时会发生这种情况。

public void initSeats(){
    for(int b = 0; b < seatList.length; b++)
    {
     initC.setName("null");
     initC.setEmail("null");
     initC.setCreditNo(0);
     initC.setAddress("null");
     initC.setPassportNo("null");
     seatList[b] = new Seat('A', 0, "null", 0.0, "Available", initC);   
    }

    for(int d = 0; d <= 24; d++)
    {
        seatList[d].setSeatLetter('A');
        seatList[d].setSeatNo(d);
    }
    for(int n = 25; n <= 48; n++)
    {
        seatList[n].setSeatLetter('B');
        seatList[n].setSeatNo(n);
    }
    for(int m = 49; m <= 72; m++)
    {
        seatList[m].setSeatLetter('C');
        seatList[m].setSeatNo(m);
    }
    for(int t = 73; t <= 96; t++)
    {
        seatList[t].setSeatLetter('D');
        seatList[t].setSeatNo(t);
    }
    for(int q = 97; q <= 120; q++)
    {
        seatList[q].setSeatLetter('E');
        seatList[q].setSeatNo(q);
    }
    for(int v = 121; v < 144; v++)
    {
        seatList[v].setSeatLetter('F');
        seatList[v].setSeatNo(v);
    }
    for(int x = 0; x <= 48; x++)
    {
        seatList[x].setSection("Front");
        seatList[x].setPrice(500);
    }
    for(int j = 49; j <= 96; j++)
    {
        seatList[j].setSection("Middle");
        seatList[j].setPrice(250);
    }
    for(int u = 97; u < 144; u++)
    {
        seatList[u].setSection("Back");
        seatList[u].setPrice(100);
    }
}
   public void seatChecker(int index)
   {
    String status = seatList[index].getStatus();
    if(status.equalsIgnoreCase("Available")){
            System.out.println("Seat is Available.");
        }else{
        System.out.println("Seat is not Available. Please Pick Another Seat.");
        bookMenu();
        }
   }

public void bookMenu()
{
    int choice1 = 0;
    int index;
    System.out.println("Where do you want to be seated?");
    System.out.println("[1] Front");
    System.out.println("[2] Middle");
    System.out.println("[3] Back");
    choice1 = sc.nextInt();
    sc.nextLine();
    if(choice1 == 1){
        System.out.print("Choose a seat number (0 - 48): ");
        index = sc.nextInt();
        sc.nextLine();
        seatChecker(index);
    }else if(choice1 == 2){
        System.out.println("Choose a seat number (49 - 96): ");
        index = sc.nextInt();
        sc.nextLine();
        seatChecker(index);
    }else if(choice1 == 3){
        System.out.println("Choose a seat number (97 - 144): ");
        index = sc.nextInt();
        sc.nextLine();
        seatChecker(index);
    }else
    {
        System.out.println("Invalid Choice. Going back to Menu.");
        MainMenu();
    }
}

空指针异常代码

Exception in thread "main" java.lang.NullPointerException
at pkg.Airlines.AirlineUI.seatChecker(AirlineUI.java:132)

座位等级

public class Seat{
private char seatLetter;
private int seatNo;
private String section;
private double price;
private String status;
private Customer customerDetails;

 public Seat(char seatLetter, int seatNo, String section, double price, String status, Customer details) 
   {
    this.seatLetter = seatLetter;
    this.seatNo = seatNo;
    this.section = section;
    this.price = price;
    this.status = status;
    this.customerDetails = details;
}

public Customer getCustomerDetails() {
    return customerDetails;
}

public void setCustomerDetails(Customer customerDetails) {
    this.customerDetails = customerDetails;
}

public char getSeatLetter() {
    return seatLetter;
}

public void setSeatLetter(char seatLetter) {
    this.seatLetter = seatLetter;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public double getPrice() {
    return price;
}

public void setPrice(double price) {
    this.price = price;
}

public int getSeatNo() {
    return seatNo;
}

public void setSeatNo(int seatNo) {
    this.seatNo = seatNo;
}

public String getSection() {
    return section;
}

public void setSection(String section) {
    this.section = section;
}

}

2 个答案:

答案 0 :(得分:0)

问题可能就是这两行:

String status = seatList[index].getStatus();
if(status.equalsIgnoreCase("Available"))

第一个事物可能seatList[index]未初始化。将引用数组声明为:

Seat[] array = new Seat[10];

该数组包含10对象的null Seat个引用。您需要在使用它们之前对它们进行实例化:

Seat[0] = new Seat();

第二个潜在问题,这个检查:

if(status.equalsIgnoreCase("Available"))

将其替换为:

if("Available".equalsIgnoreCase(status))

以避免在NullPointerExceptionstatus的情况下null {。}}。

P.S。请向我们展示Seat课程,以便更好地了解您的问题。

答案 1 :(得分:0)

很简单就解决了空指针异常。 可能在seatList的索引之一中没有值。