这是我正在做的学校项目。我需要从文件中获取数据并使用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;
}
当我测试它时,我收到此错误:
Exception in thread "main" java.lang.NullPointerException
at project6driver.Project6Driver.readFile(Project6Driver.java:36)
at project6driver.Project6Driver.main(Project6Driver.java:22)
36是我第一次在for循环中使用setter方法。我不明白为什么这不起作用,有人能指出我正确的方向吗?