2017-02-12 36 views
0

我在嘗試將數組對象分配給另一個數組時遇到了NPE。我在這裏做錯了什麼?NPE卡住將Array.toString(對象)分配給另一個數組中的類

這是我的工作代碼:

import java.util.Scanner; 
import java.util.Arrays; 

public class groupData { 
    public static void main(String[] args) { 
     Group students = new Group(4); 
     students.promtNewUser(); 
    } 
} 

class Group { 
    Scanner input = new Scanner(System.in); 
    private int user_count; 
    private int max_users;//maximum number of users 
    private String[] directory;//array for list of users 
    private String[] user;//array for ind users 

    //constructor method 
    public Group(int total_users) { 
     max_users = total_users;//max_users equals value passed to class with new 
     user_count = 0; 
     String[] directory = new String[max_users]; 
    } 

    public void promtNewUser() { 
     String[] user = new String[4]; 

     System.out.print("Enter first name: "); 
     String fname = input.nextLine(); 
     user[0] = fname; 

     System.out.print("Enter last name: "); 
     String lname = input.nextLine(); 
     user[1] = lname; 

     System.out.print("Enter phone number: "); 
     String phone = input.nextLine(); 
     user[2] = phone; 

     System.out.print("Enter age: "); 
     String age = input.nextLine(); 
     user[3] = age; 

     add_user(user); 
    } 

    public void add_user(String[] user) { 
     if (user_count == max_users) { 
     System.out.println("Sorry, the group is full!"); 
     } 
     else { 
     directory[user_count] = Arrays.toString(user); 
     System.out.println("User added to group!"); 
     user_count++; 
     } 

    } 

}

這裏是在終端輸出通過提示編譯並運行後:

Enter first name: Winston 
Enter last name: Churchill 
Enter phone number: +44 1 3425 9989 834 
Enter age: 143 
Exception in thread "main" java.lang.NullPointerException 
    at Group.add_user(groupData.java:53) 
    at Group.promtNewUser(groupData.java:45) 
    at groupData.main(groupData.java:8) 
+0

抱歉,這是一個嚴重的代碼和一個嚴重的措辭問題。 –

回答

1

在你Group您指定的類別String[] directory

class Group { 
    // ... 
    private String[] directory;//array for list of users 

然後在你的Group構造,聲明一個新的局部變量具有相同的名稱,從而有效地隱藏類變量:

//constructor method 
public Group(int total_users) { 
    // ... 
    String[] directory = new String[max_users]; 

當你構建你的Group,類變量永遠不會被初始化,並保持null和局部變量被創建,分配和從不使用。再後來就嘗試索引類directory變量,但它是null

public void add_user(String[] user) { 
    if (user_count == max_users) { 
     System.out.println("Sorry, the group is full!"); 
    } 
    else { 
     directory[user_count] = Arrays.toString(user); // HERE: directory is null 
     System.out.println("User added to group!"); 
     user_count++; 
    } 
} 

通過初始化而不是一個局部變量的類變量修復Group構造:

//constructor method 
public Group(int total_users) { 
    max_users = total_users;//max_users equals value passed to class with new 
    user_count = 0; 
    directory = new String[max_users]; // removed the String[] type, so you are now referencing the class variable 
} 
+0

謝謝馬特!這解決了它。當我現在使用你的建議運行我的程序時,我得到:'用戶添加到組!!感謝你的幫助。 – Fergus

相關問題