我是 Java 新手,谁能解释为什么我得到一个空指针异常?此外,任何人都可以解释除 Scanner 之外的其他可靠输入方法。
错误
线程“主” java.lang.NullPointerException中的异常
在 Theatre.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]);
}
}