【问题标题】:NullPointerException, multidimensional arrayNullPointerException,多维数组
【发布时间】:2015-10-07 21:19:26
【问题描述】:

我是 Java 新手,我正在为我的计算机科学课做一个项目。 我必须向多维数组添加细节。这是我的代码

public class Login {
public static String[][] all_users = new String[50][13];

public static void main(String[] args){ //throws FileNotFoundException {
    welcome();
    userSearch user_info = new userSearch();
    user_info.fileSearch(all_users);
    Scanner userIn = new Scanner(System.in);
    String input = userIn.next();
    switch (input) {
        case "1":
            user_info.viewUserList(all_users);
            break;
        case "2":
            AddUser.addDetails();
            break;
        case "3":
            System.out.println("please enter administrator password to       modify or delete contents: ");
            Pass.getPassword();
            break;
    }
}

private static void welcome() {
    System.out.println("Welcome to the user information profiler. \n here are your options, please enter one of the numbers: ");
    System.out.println("1) view a list of all current users. /n 2) Add user details to the list."
            + "/n 3) if you know the administrator password and wish to modify or delete user details.");
    System.out.println("Enter a choice now: ");
}        

}

和添加用户类

public class AddUser extends Login {
public static void addDetails(){
System.out.println("Please add details for a new user: ");
    for (int i = 0; i < all_users.length; i++){
        if (all_users [i][0] == null){
            for (int j = 0; j < 13; j++){
                Scanner scan = new Scanner(System.in);
                switch(all_users[i][j]) {
                    case "1":
                        System.out.println("input first name: ");
                        all_users[i][0] = scan.next();
                        break;
                    case "2":
                        System.out.println("input last name: ");
                        all_users[i][1] = scan.next();
                        break;
                    case "3":
                        System.out.println("input phone number(xxxyyyzzzz): ");
                        all_users[i][2] = scan.next();
                        break;
                    case "4":
                        System.out.println("input email address: ");
                        all_users[i][3] = scan.next();
                        break;
                    case "5":
                        System.out.println("input Apartment Number: ");
                        all_users[i][4] = scan.next();
                        break;
                    case "6":
                        System.out.println("input Street Adress: ");
                        all_users[i][5] = scan.next();
                        break;
                    case "7":
                        System.out.println("input City: ");
                        all_users[i][6] = scan.next();
                        break;
                    case "8":
                        System.out.println("input State (ex. NJ): ");
                        all_users[i][7] = scan.next();
                        break;
                    case "9":
                        System.out.println("input Zip Code: ");
                        all_users[i][8] = scan.next();
                        break;
                    case "10":
                        System.out.println("input Vehicle Type(ex. luxury, sport): ");
                        all_users[i][9] = scan.next();
                        break;
                    case "11":
                        System.out.println("input Vehicle Model(ex. Toyota, Nissan: ");
                        all_users[i][10] = scan.next();
                        break;
                    case "12":
                        System.out.println("input Vehicle Color: ");
                        all_users[i][11] = scan.next();
                        break;
                    case "13":
                        System.out.println("input License Plate: ");
                        all_users[i][12] = scan.next();
                        break;
            }
            }
        }
    }
}
}

我得到一个 NullPointerException

switch(all_users[i][j]) {

继续

AddUser.addDetails();

为什么会发生这种情况,我该如何解决?

【问题讨论】:

  • 什么是 all_users,为什么您认为 AddUser 可以访问它?
  • all_users 是一个二维数组,用于存储文件中的信息。

标签: java multidimensional-array nullpointerexception


【解决方案1】:
if (all_users [i][0] == null){
    for (int j = 0; j < 13; j++){

只有当 [i][0]null 时,你才进入,但是当 j = 0 时你正在切换 [i][j]。所以你第一个测试的值是 null

替换

for (int j = 0; j < 13; j++){

for (int j = 1; j < 13; j++){

【讨论】:

  • 感谢您的回复,但是,这似乎无法解决错误。无论如何,如果我更改为 j=1,那么我无法写入数组的索引 [i][0]。所以这不会有帮助。不过谢谢你的回答!
  • 在切换之前将写入添加到索引零...不要忘记接受答案:)
猜你喜欢
  • 1970-01-01
  • 2013-06-26
  • 2023-03-19
  • 2018-03-05
  • 2010-12-19
  • 2013-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多