0

I am new to Java and I am doing a project for my computer science class. I have to add details to a multi-dimensional array. Here is my code

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: ");
}        

}

and the Add user class

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;
            }
            }
        }
    }
}
}

I get a NullPointerException

switch(all_users[i][j]) {

and on

AddUser.addDetails();

Why is this happening, and how can i fix it?

2
  • What is all_users and why do you think AddUser would have access to it? Commented Oct 7, 2015 at 21:29
  • all_users is a 2d array that stores information from a file. Commented Oct 7, 2015 at 21:30

1 Answer 1

1
if (all_users [i][0] == null){
    for (int j = 0; j < 13; j++){

You're entering only if [i][0] is null but then you're switching [i][j] when j = 0. So you're first value tested is null

replace

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

with

for (int j = 1; j < 13; j++){
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your reply, however, that does not seem to fix the error. Regardless, if i change to j=1, then i cant write to index [i][0] of the array. So that would not have helped. but thank you for your answer!
Before your switch add the writing to the index zero... Do not forget to accept the answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.