Java读取输入并分配给类

Java读取输入并分配给类,java,Java,这是我正在做的一个学校项目。我需要从文件中获取数据,并使用setter方法将行的下一部分分配给类的不同部分。我必须为数组中的多个实例分配所有这些信息 public static void main(String[] args) throws Exception { //declarations and input Scanner input = new Scanner(System.in); System.out.println("Enter the file to re

这是我正在做的一个学校项目。我需要从文件中获取数据,并使用setter方法将行的下一部分分配给类的不同部分。我必须为数组中的多个实例分配所有这些信息

public static void main(String[] args) throws Exception {
    //declarations and input
    Scanner input = new Scanner(System.in);
    System.out.println("Enter the file to read from: ");
    String fileIn = input.next();
    System.out.println("Enter the number of accounts in the file: ");
    int number = input.nextInt();
    System.out.println("Enter the file to output the distribution list to: ");
    String fileOut = input.next();

    //call function
    Account[]students = (readFile(fileIn, number));
    System.out.println("Done with File");

}
public static Account[] readFile(String file, int column) throws Exception
{
    File myFile = new File(file);
    Scanner fileInput = new Scanner(myFile);
    Account[]students = new Account[column];
    while(fileInput.hasNext())
    {
        for(int i=0; i<=column; i++)
        {
            students[i].setID(fileInput.nextInt());
            students[i].setName(fileInput.next());
            students[i].setUsername(fileInput.next());
            students[i].setPassword(fileInput.next());
            students[i].setEmail(students[i].getUsername()+"@mail.nwmissouri.edu");
        }
    }
    return students;
}

36是我第一次在for循环中使用setter方法。我不明白这为什么不起作用,有人能告诉我正确的方向吗?

用Java创建数组时,它是空的。您无法尝试在
null
上设置值,因此您需要实际创建
Account
对象,并将其放入数组中,然后再尝试对其进行操作。可能重复的注意事项是
学生
是一个帐户数组。这意味着您需要使用Account对象设置数组的每个索引。因此,在循环中,您需要执行一个
Account student=new Account()
,然后执行
student.setID(…)
等操作。然后执行
students[i]=student
try
for(int i=0;这也可以归结为您的数据文件(您从中读取的数据文件)的方式)布局。数据文件中一行的典型布局是什么?它是否像
ID:..Name:..Username:..
Exception in thread "main" java.lang.NullPointerException
at project6driver.Project6Driver.readFile(Project6Driver.java:36)
at project6driver.Project6Driver.main(Project6Driver.java:22)