0

我想创建一个 2D 数组来创建飞机的迷你座位图。到目前为止,我已经成功打印出如下所示的内容:

1A(0) || 1B(0) || 1C(0)

2A(0) || 2B(0) || 2C(0)

3A(0) || 3B(0) || 3C(0)

4A(0) || 4B(0) || 4C(0)

零代表一个空座位,数字一代表一个被占用的座位。

我首先使用作为头等舱类变量的数组创建了程序,但我想让这个程序可用于经济舱部分。这两个部分之间的唯一区别是数组的大小,因此我将代码编辑为如下所示:

public class Seating
 { 
 private int FIRSTCLASS= 12; 
 private int ECONOMYCLASS= 240;
 private int occupied, column;
 private String[][] seatchart;
 private int[][] seatlist;
 private String[][] namelist;
 private String name; 
 public String customer;

public Seating(String seatclass) 
{ 
    seatclass.toUpperCase();
    if (seatclass.equals("FIRSTCLASS"))
    { 
      seatchart= new String[FIRSTCLASS/3][3];
      seatlist= new int[FIRSTCLASS/3][3];
      namelist= new String[FIRSTCLASS/3][3];
    }
    else 
    if (seatclass.equals("ECONOMY"))
    { 
      seatchart= new String[ECONOMYCLASS/3][3];
      seatlist= new int[ECONOMYCLASS/3][3];
      namelist= new String[ECONOMYCLASS/3][3];
    }   

}
public void Creation()
 {
   for (int i=0; i< seatlist.length; i++) 
    {  
        for (int j=0; j<seatlist[i].length; j++) 
        { 
            seatlist[i][j]= 0 ;

        }
    }

我收到一个空指针异常错误, for (int i=0; i< seatlist.length; i++) 我该如何解决这个错误?

提前致谢!

4

2 回答 2

2

代码行可以生成 NPE 的唯一方法是 if seatlistis null。除非您分配nullseatlist类中的其他位置,否则唯一的方法null是如果您传递给构造函数的参数与orSeating不匹配。检查您对构造函数的调用。此外,您可能只想使用."FIRSTCLASS""ECONOMY"seatclass.equalsIgnoreCase()

您应该修改您的构造函数以至少警告这种可能性,因为任何实例都Seating具有有效seatlistnamelist数组对于类的正确操作至关重要。

于 2013-04-08T03:05:55.827 回答
2

问题在于这一行:

seatclass.toUpperCase();

将其替换为:

seatclass = seatclass.toUpperCase();

我认为您正在使用“firstclass”而不是“FIRSTCLASS”之类的字符串创建课程,对吗?这些不是相同的字符串,只是在字符串上调用 toUpperCase 方法而不将结果分配给要测试的变量意味着什么都不会发生。

然后,由于没有满足您的 if 条件,因此未初始化数组,并且在调用 Completion() 时会引发空指针异常。

我不确定您是否是 Java 编程新手,但我想向您的课程添加一些建议:

public class Seating {

 private static int FIRSTCLASS= 12;    // Make these constants static since they pertain to all 
 private static int ECONOMYCLASS= 240; // instances of your class. That way there is exactly on
                                       // copy of the variables, which is more memory efficient.
 private int occupied;
 private column;  // Okay but Java convention is to declare each member variable on its own line
                  // increases code readability.
 private String[][] seatchart;
 private int[][] seatlist;
 private String[][] namelist;
 private String locSeatClass;
 private String name;

 public String customer; // Okay but better to leave this private and then provide getter and
                         // setter methods to provide access to this string. Much easier to track
                         // down who is changing its value in your code.

public Seating(String seatclass) { // Java convention is to place the opening bracket here not
                                   // on the next line.  
    // Make sure that seatClass is not null or empty. NOTE: This is a neat trick for
    // simultaneously checking for both null and empty strings in one shot. Otherwise, you have
    // you have to check for null and then examine the string's length which is more code.
    if ("".equalsIgnoreCase(seatClass) {
        throw new IllegalArgumentException("Seat class undefined.");
    }

    // Store the seat class in a member variable for use. Could also be a local variable.
    // My original solution is problematic because it changes the original value of the seat
    // class that was passed into the constructor (you might want that information).
    locSeatClass = seatclass.toUpperCase();

    if (locSeatClass.equals("FIRSTCLASS"))
    { 
      seatchart= new String[FIRSTCLASS/3][3];
      seatlist= new int[FIRSTCLASS/3][3];
      namelist= new String[FIRSTCLASS/3][3];
    }
    else if (locSeatclass.equals("ECONOMY")) { 
      seatchart= new String[ECONOMYCLASS/3][3];
      seatlist= new int[ECONOMYCLASS/3][3];
      namelist= new String[ECONOMYCLASS/3][3];
    }
    else {
        // Throw an exception if someone passes in an unknown seat class string.
        throw new IllegalArgumentException("Unknown seat class detected.")
    }  

}

public void creation() { // NOTE: Java convention is to begin method names with a lower 
                         // case letter.

   // This method is unnecessary. Arrays of integers are initialized with an initial value
   // of zero by default. However, if you want to make your class reusable, you could change
   // change the name of the this method to clear, which would allow you to clear the arrays of
   // an existing object.
   for (int i=0; i< seatlist.length; i++) 
    {  
        for (int j=0; j<seatlist[i].length; j++) 
        { 
            seatlist[i][j]= 0 ;

        }
    }

}

于 2013-04-08T03:09:47.827 回答