为什么我在这个程序中得到nullpointer异常?

时间:2013-03-12 15:11:21

标签: java nullpointerexception

我是Java的新手,任何人都可以解释为什么我会得到空指针异常? 此外,任何人都可以解释扫描仪以外的其他可靠输入方法。

错误

  

线程中的异常" main" 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]);
    }
}

7 个答案:

答案 0 :(得分:2)

您需要填充您的Scrrens array。换句话说,你只是初始化你的Scrren数组但从未初始化它的元素。如果你没有初始化它的元素,它们会得到默认值,在这种情况下是 null 。猜猜在null上调用somemethod时会发生什么。你得到了强烈的 NPE

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

答案 1 :(得分:2)

虽然初始化数组X但是你没有初始化它的成员所以在执行X[Screen-1]时你正在使用一个空对象(即使索引在边界内)。

答案 2 :(得分:2)

因为数组X中没有任何元素,所以默认情况下每个元素都会初始化为null。所以基本上你试图这样做null.bookseat(),结果是NullPointerException

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

答案 3 :(得分:2)

你得到一个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();
}

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

答案 4 :(得分:2)

正在创建数组X,但元素未初始化,它们仍为空。我想你希望你的数组初始化如下:

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

答案 5 :(得分:1)

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

答案 6 :(得分:1)

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

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

您可能会发现使用集合中的List更有用。