繁体   English   中英

为什么在此程序中出现nullpointer异常?

[英]Why am I getting nullpointer Exception in this Program?

我是Java的新手,谁能解释我为什么会收到Null指针异常 此外,任何人都可以解释除Scanner之外的其他可靠输入法。

错误

线程“主”中的异常java.lang.NullPointerException

在Theater.main( Theater.java:18

import java.util.Scanner;
public class Theater 
{
    public static void main(String[] args) throws Exception 
    {
        int Screen;
        Screens[] X = new Screens[5];
        Scanner input = new Scanner(System.in);
        do
        {
            System.out.println();
            System.out.println(" '0' to exit");
            System.out.println(" '1-5' for Booking Seats");
            System.out.println(" '10' for Displaying Seating Status");
            System.out.println("Enter Screen Number : ");
            Screen = input.nextInt();
            if(Screen >= 1 && Screen <= 4)
                X[Screen-1].bookSeat();
            else if(Screen == 0)
            {
                System.out.println("Thank You for Booking Seats in PVR Cinemas.");
                System.in.read();
                System.exit(0);
            }
        }while(true);

    }
}
class Screens
{
    private
        int[] Gold = new int[3];
        int[] Platinum = new int[3];
        int[] Diamond = new int[3];
        int g,d,p;
        Scanner input = new Scanner(System.in);
        final int MAX=3;
    public  Screens()
    {
        for( int i=0;i<3;i++)
        {
            Gold[i] = 0;
            Platinum[i] = 0;
            Diamond[i] = 0;
            g = d = p = 0;
        }
    }
    public void bookSeat()
    {
        int n=0,choice,i;
        System.out.println("\t\tMenu");
        System.out.println("1.Gold \tAvailable Seats : "+(3-g));
        System.out.println("2.Platinum \tAvailable Seats : "+(3-p));
        System.out.println("3.Diamond \tAvailable Seats : "+(3-d));
        System.out.println("4.Return to Main Menu");
        System.out.println("Your Choice : ");
        choice = input.nextInt();
        if(choice>=1 && choice<=3)
        {
                    System.out.print("How many Seats ? : ");
                    n = input.nextInt();
                    if( n<=0 )
                    {
                        System.out.println("Please Check your Input.");
                        return;
                    }
                    else if( n>=MAX )
                    {
                        System.out.println("The Maximum Number of Seats is : "+MAX);
                    }
        }
        switch(choice)
        {
            case 1:
                if(g+n >3)
                {
                    System.out.println("Housefull!");
                    break;
                }
                else
                {
                    int total = 0;
                    System.out.print("Seat Numbers are : ");
                    for(i=0;i<n;i++)
                    {
                        Gold[g++] = 1;
                        System.out.print("\t"+g);
                    }
                    total = 100 * n;
                    System.out.println("Total Money to be paid : "+total);
                }
            break;
            case 2:
                if(p+n >3)
                {
                    System.out.println("Housefull!");
                    break;
                }
                else
                {
                    int total = 0;
                    System.out.print("Seat Numbers are : ");
                    for(i=0;i<n;i++)
                    {
                        Platinum[p++] = 1;
                        System.out.print("\t"+p);
                    }
                    total = 125 * n;
                    System.out.println("Total Money to be paid : "+total);
                }
            break;
            case 3:
                if(d+n >3)
                {
                    System.out.println("Housefull!");
                    break;
                }
                else
                {
                    int total = 0;
                    System.out.print("Seat Numbers are : ");
                    for(i=0;i<n;i++)
                    {
                        Diamond[d++] = 1;
                        System.out.print("\t"+d);
                    }
                    total = 150 * n;
                    System.out.println("Total Money to be paid : "+total);
                }
            break;
            case 4:
            break;
            default:
                System.out.println("Sorry, That's an invalid Choice!");

        }
        return;
    }
    public void viewSeats()
    {
        int i;
        System.out.println("Gold Category : ");
        for(i=0;i<3;i++)
        System.out.print("\t "+Gold[i]);
        System.out.println("Platinum Category : ");
        for(i=0;i<3;i++)
        System.out.print("\t "+Platinum[i]);
        System.out.println("Diamond Category : ");
        for(i=0;i<3;i++)
        System.out.print("\t "+Diamond[i]);
    }
}

您需要填充 Scrrens array 换句话说,您只是初始化了Scrren数组而没有初始化它的元素。如果您不初始化它的元素,它们将获得默认值,在这种情况下为null 猜测在null上调用某种方法时会发生什么。 繁荣的NPE

Screens[] X = new Screens[5];
x[0] = new Screen();

尽管初始化数组X并不会使数组的成员成整数,所以在执行X[Screen-1]您将使用空对象(即使索引位于边界中)。

由于数组X中没有任何元素,因此默认情况下,每个元素都初始化为null 因此,基本上,您正在尝试执行null.bookseat() ,这会导致NullPointerException

同样重要的是要注意,如果Screen5 (您的条件<= 5允许),那么您将获得ArrayIndexOutOfBoundsException因为您的数组仅具有索引0,1,2,3,4 (共5个)

之所以收到NullPointerException,是因为您已声明一个数组来容纳5个屏幕对象,但从未用实际的Screen对象初始化这5个插槽

因此,当您尝试使用

X[Screen-1].bookSeat();

您引用数组中的null元素,当然您不能调用null对象的方法

您可以在使用对象之前添加检查并初始化屏幕

if(Screen >= 1 && Screen <= 4) {
    if (X[Screen-1] == null) 
       X[Screen-1] = new Screens();

    X[Screen-1].bookSeat();
}

在您的使用中也有些奇怪。 数组从零索引开始,但是您使用零作为从程序中退出的值,因此从不使用索引为零的元素。

您的数组X正在创建,但是元素没有初始化,它们仍然为null。 我认为您期望像这样初始化您的数组:

Screens[] X = new Screens[5];
for (int x = 0; x < 5; x++) {
    X[x] = new Screens();
}

因为Screens[] X = new Screens[5]; 数组X的所有5个元素都为空!

另一种填充屏幕数组的方法

Screens[] X = { new Screens(), new Screens(), new Screens(),new Screens(), new Screens() };

您可能会发现使用来自收藏夹的列表”更有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM