0

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?

  • 1
    When you create an array in Java, it's empty. You can't try to set values on `null`, so you need to actually create `Account` objects and put them in the array before you try to act on them. – azurefrog Mar 07 '16 at 18:35
  • Possible duplicate of [NullPointerException when Creating an Array of objects](http://stackoverflow.com/questions/1922677/nullpointerexception-when-creating-an-array-of-objects) – azurefrog Mar 07 '16 at 18:37
  • Notice that `students` is an Array of Accounts. This means you need to set each index of the Array with an Account object. So in your loop, you need to do a `Account student = new Account()`, then do `student.setID(...)` etc. Then you do `students[i] = student` – Ascalonian Mar 07 '16 at 18:38
  • try `for(int i=0; i – koem Mar 07 '16 at 18:38
  • This also boils down to how your data file (the one you read from) is laid out. What is the typical layout of one line from the data file? Is it like `ID:.... Name:....Username:....`? – A.Sharma Mar 07 '16 at 18:44
  • file layout looks like 919123456 Bob Smith s123456 Jkw78bnm – JimmyGatz Mar 07 '16 at 18:50
  • Hey it looks like my error was not creating the account objects before I set values for them. Thanks for the prompt help! – JimmyGatz Mar 07 '16 at 18:56

0 Answers0