This is for a school project that I'm doing. I need to take data from a file and use setter methods to assign the next part of the line to different parts of a class. I have to assign all of that information for multiple instances in an array.
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;
}
When I test it out I get this error:
Exception in thread "main" java.lang.NullPointerException
at project6driver.Project6Driver.readFile(Project6Driver.java:36)
at project6driver.Project6Driver.main(Project6Driver.java:22)
36 is the first time I'm using the setter method inside my for loop. I don't understand why this doesn't work, can somebody point me in the right direction?