[英]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
。
同樣重要的是要注意,如果Screen
為5
(您的條件<= 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個元素都為空!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.