【问题标题】:Unknown NullPointerException未知的 NullPointerException
【发布时间】:2013-12-06 21:33:42
【问题描述】:

我正在编写一个简单的类来模拟飞机预订系统。不幸的是,我的计划一直被多个空指针异常打断,我在任何地方都找不到它们。这是我完成的工作:

import java.util.*;
public class airplane
{
    boolean[] seats;
    Scanner scan = new Scanner(System.in);
    public airplane(int num_of_seats)
    {
        boolean[] seats = new boolean[num_of_seats];
        for (int counter = 0; counter < num_of_seats; counter++)
        {
            seats[counter] = true;
        }
    }
    public boolean seatFirstClass(int seat)
    {
        if ( seat <= seats.length / 4 )
            return true;
        else
            return false;
    }
    public void seatsReserveFirstClass()
    {
        for (int seat = 0; seat < seats.length; seat++)
        {
            if (seats[seat] == true && seat <= seats.length / 4)
            {
                seats[seat] = false;
                System.out.print("I have reserved one seat in first class. Please tell the passenger to enjoy their flight with these airlines! ");
                break;
            }
        }
        System.out.print("I could not find a seat in first class. Can the passenger switch to economy? (0 for no, 1 for yes) ");
        int choice = scan.nextInt();
        if (choice == 0)
            System.out.print("I could not add passenger due to lack of a seat.");
        else if (choice == 1)
        {
            this.seatsReserveEconomy();
        }
    }
    public void seatsReserveEconomy()
    {
        for (int seat = 0; seat < seats.length; seat++)
        {
            if (seats[seat] == true && seat > seats.length / 4)
            {
                seats[seat] = false;
                System.out.print("I have reserved a seat in economy. Please tell the passenger to enjoy their flight with these airlines! ");
                break;
            }
        }
        System.out.print("I could not find a seat in economy. Can the passenger switch to first class? (0 for no, 1 for yes) ");
        int choice = scan.nextInt();
        if (choice == 0)
            System.out.print("I could not add the passenger due to a lack of seats.");
        else if (choice == 1)
            this.seatsReserveFirstClass();
    }
    public void planeClear()
    {
        for (int seat = 0; seat < seats.length; seat++)
        {
            seats[seat] = true;
        }
    }
    public void seatUnReserve(int seat)
    {
        if (seat < seats.length)
        {
            if (seats[seat] == false)
            {
                seats[seat] = true;
                System.out.printf("Seat %d has been unreserved.\n", seat);
            }
            else
            {
                System.out.print("The seat you have entered is already unreserved.\n");
            }
        }
        else
        {
            System.out.printf("There is no seat number %d on this plane.\n", seat);
        }
    }
}

这是一个测试它的主类:

public class Test
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        boolean check = false;
        System.out.print("How many seats does the plane have? ");
        int num_of_seats = scan.nextInt();
        airplane Airplane = new airplane(num_of_seats);
        while (check == false)
        {
            System.out.print("Please enter 0 to reserve a first class seat, 1 to reserve an economy seat,\n2 to cancel a reservation, 3 to clear the plane,\nand 4 to quit the program. ");
            int choice = scan.nextInt();
            switch(choice)
            {
            case 0:
               Airplane.seatsReserveFirstClass();
               break;
            case 1:
                Airplane.seatsReserveEconomy();
                break;
            case 2:
                System.out.print("Which seat should I unreserve? ");
                int seat = scan.nextInt();
                Airplane.seatUnReserve(seat);
                break;
            case 3:
                Airplane.planeClear();
                break;
            case 4:
                check = true;
                break;
            }
        }
    }

}

如果运行,会出现可怕的 NullPointException 错误,我不知道什么叫这个野兽。

Exception in thread "main" java.lang.NullPointerException
at airplane.seatsReserveFirstClass(airplane.java:22)
at Test.main(Test.java:15)

【问题讨论】:

  • "...the dreaded NullPointException error shows up, and I'm not sure what calls this beast." -- 异常堆栈跟踪会告诉你!你的堆栈跟踪在哪里,它告诉你哪一行抛出了 NPE?不要忽略异常消息——研究它们!
  • test.main(test.java:58) 处飞机.seatsReserveFirstClass(airplane.java:63) 处的线程“main”java.lang.NullPointerException 中的异常
  • 那么飞机.java 63 是哪一行?同样,这些消息会告诉您足够的信息来寻找解决方案,尽管您的答案可能低于 Hernan 的答案。关键是不要忽略异常堆栈跟踪消息,因为它们是关键。如果您想帮助其他人理解您的代码,请学习使用 Java 命名约定。
  • 啊,对不起。我不知道(对java来说是全新的)。谢谢,很高兴知道。

标签: java arrays nullpointerexception


【解决方案1】:

这是一个字段成员:

boolean[] 席位;

Scanner scan = new Scanner(System.in);
public airplane(int num_of_seats)
{

在这里你用一个局部变量来隐藏字段成员,你没有触及另一个

    boolean[] seats = new boolean[num_of_seats];

    for (int counter = 0; counter < num_of_seats; counter++)
    {
        seats[counter] = true;
    }
}

应该是这样的:

public airplane(int num_of_seats)
{
    seats = new boolean[num_of_seats];
    for (int counter = 0; counter < num_of_seats; counter++)
    {
        seats[counter] = true;
    }
}

【讨论】:

    【解决方案2】:

    Herman 的快速眼睛已经为您找到答案了!!无论如何,我会为未来提供帮助......

    你可以使用Java IDE,eclipse是不错的,开发程序。我相信其中一个非常重要的特性是调试,它使您能够完全控制您的应用程序并快速跟踪异常。

    这里是如何从 Eclipse 调试应用程序的快速教程(请参阅 4.3 的异常断点)

    http://www.vogella.com/articles/EclipseDebugging/article.html

    【讨论】:

      猜你喜欢
      • 2014-06-11
      • 2016-12-31
      • 1970-01-01
      • 2020-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-26
      • 2012-09-15
      相关资源
      最近更新 更多