This code should create an array of Student objects where each Student object contains fields consisting of the first name, last name and average score for the 3 exams. It will then iterate through that array and print out the values of those fields. I can't change anything in the "main" method because it's part of an assignment. I'm getting a NullPointerException on line 59. I can't figure out what why I'm getting this error.
Exception in thread "main" java.lang.NullPointerException at ProgramIII.populateStudentArray(ProgramIII.java:59)
Here is the Student class...
This is the format of the file I'm supposed to read from. It will not change.
If anybody could help me out that would be great. Thank you!
Next time, please don't add your own line numbers. They will be added automatically once you add code tags, and by adding them yourself you made it quite hard for us to simply copy-paste-compile-run your code.
However, I think I already found the problem:
studentArray is an array with of size "number" with all null elements. You need to initialize them first, either in createStudentArray or just before using an array element in populateStudentArray. Let's do it in createStudentArray:
Please don't post line numbers; the code tags add them automatically. Also please keep exactly the same formatting; I ran your code and got the NullPointerException at line 58! Insert this line before the line which sufferes the Exception . . . and see its output.
Joined: Oct 13, 2005
. . . and as Rob said, you find the array exists, but the individual members are still null.
Joined: Mar 15, 2011
Sorry about that, I won't do it again. I took your suggestion and tried running my code with
that line implemented and it worked. Thank you for the help!