可编辑的2D阵列

时间:2013-04-08 02:58:09

标签: java arrays class for-loop nullpointerexception

我想创建一个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)

零代表一个空座位,第一个代表占用座位。

我首先使用数组创建程序,这些数组是First Class的类变量,但我想让这个程序可用于经济类部分。这两个部分之间的唯一区别是数组的大小,所以我编辑了我的代码:

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++)周围出现空指针异常错误 我该如何解决这个错误?

提前致谢!

2 个答案:

答案 0 :(得分:2)

代码行生成NPE的唯一方法是seatlistnull。除非您在类中的其他位置将null分配给seatlist,否则null的唯一方法就是传递给Seating构造函数的参数不匹配"FIRSTCLASS""ECONOMY"。检查你对构造函数的调用。此外,您可能只想使用seatclass.equalsIgnoreCase()

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

答案 1 :(得分:2)

问题在于这一行:

seatclass.toUpperCase();

将其替换为:

seatclass = seatclass.toUpperCase();

我认为你用类似“firstclass”而不是“FIRSTCLASS”的字符串来创建类吗?那些不是相同的字符串,只是在字符串上调用toUpperCase方法而不将结果分配给变量然后进行测试意味着没有任何反应。

然后,由于没有满足任何条件,因此在调用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 ;

        }
    }

}