2013-04-08 92 views
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++) 一个空指针异常错误如何解决这个问题?

在此先感谢!

回答

2

问题是这一行:

seatclass.toUpperCase(); 

将其替换为:

seatclass = seatclass.toUpperCase(); 

我觉得你与像“的Firstclass”而不是“的Firstclass”右一个字符串创建类?这些不是相同的字符串,只是在字符串上调用toUpperCase方法而不将结果赋值给变量,然后进行测试意味着什么都不会发生。

然后因为没有你,如果条件得到满足,该阵列没有初始化完成时()被调用空指针异常被抛出。

我不知道,如果你是新的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 ; 

     } 
    } 

}

+0

谢谢!我意识到这一点后,我纠正它编译和工作正常:) – nichi 2013-04-08 03:21:26

+0

严,没什么大不了的,但我认为我的答案是答案。霍普先生的确是个好建议。 – CBass 2013-04-08 03:24:52

+0

对不起,我看到你的之前就接受了他的回答!我不知道你可以改变选择的答案,但我改变了它:) – nichi 2013-04-08 03:37:58

2

该行代码可以产生一个NPE的唯一途径是如果seatlistnull。除非您在班级中的其他地方指定nullseatlist,否则唯一的方法是null是您传递给Seating构造函数的参数不匹配"FIRSTCLASS""ECONOMY"。检查您对构造函数的调用。另外,您可能只想使用seatclass.equalsIgnoreCase()

你应该修改你的构造函数至少警告有关可能发生的,因为它是对类的Seating任何情况下都有效seatlistnamelist阵列的正常运行至关重要。

+0

我没有在我的代码仔细看和我在代码中误用了.toUpperCase()。我会记得仔细看下我的代码..并感谢提示:) – nichi 2013-04-08 03:20:19