2013-03-12 98 views
0

我是新来的Java,任何人都可以解释为什么我得到一个空指针异常? 另外任何人都可以解释其他可靠输入法除了扫描仪。为什么我在本程序中获得空指针异常?

的误差

异常螺纹“主”显示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]); 
    } 
} 

回答

2

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

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

虽然你初始化数组X你不这样做X[Screen-1]当您使用空对象(即使该指数是在bounderies)及其成员initailize左右。

2

由于您的数组X中没有任何元素,因此默认情况下每个元素都被初始化为null。所以基本上你试图做到这一点null.bookseat()这导致NullPointerException

同样重要的是要注意,如果Screen是有史以来5(这是由你的病情<= 5允许的),那么你会得到一个ArrayIndexOutOfBoundsException因为您的阵列只指数0,1,2,3,4(共5个)

+0

谢谢,我没注意到。 – Ravitheja 2013-03-12 15:21:05

2

你得到一个NullPointerException因为你已经宣告一个数组来保存5点屏幕的对象,但是当你尝试使用

X[Screen-1].bookSeat(); 
你从来没有与实际的屏幕对象初始化的5个时隙

因此3210

您在数组中引用一个null元素,当然,你不能叫一个空对象的方法

你可以使用对象之前添加一个检查和初始化屏幕

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

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

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

+0

对不起,但如何初始化?一个例子可能有帮助。 – Ravitheja 2013-03-12 15:20:06

2

正在创建您的数组X,但元素未初始化,它们仍然为空。我想你期待有你的阵列这样的初始化:

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

我无法理解,例如在C++中,如果我有一个名为Screen的类,并且有像Screen X [5]这样的语句;这意味着X是一个大小为5的Screen类的对象数组,那么这个X [x]是什么?请详细说明。 – Ravitheja 2013-03-12 15:19:03

+1

谢谢老兄,我明白你的观点。 – Ravitheja 2013-03-12 15:24:49

1

因为Screens[] X = new Screens[5];后阵列X的所有5个元素都为空!

1

另一种方式来填充你的屏幕阵列

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

您可能会发现使用List从集合更为有用。

+0

感谢兄弟,这对我来说是新的。 – Ravitheja 2013-03-12 16:51:43