2017-02-12 37 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

相关问题