Forums Register Login

NullPointerException

+Pie Number of slices to send: Send
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)
at ProgramIII.main(ProgramIII.java:33)




Here is the Student class...


This is the format of the file I'm supposed to read from. It will not change.

2

Joe
Student
80
90
100

Jill
Student
85
95
100

If anybody could help me out that would be great. Thank you!
+Pie Number of slices to send: Send
Welcome to the Ranch!

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:

+Pie Number of slices to send: Send
Welcome to the Ranch

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.
+Pie Number of slices to send: Send
. . . and as Rob said, you find the array exists, but the individual members are still null.
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
You're welcome
Every plan is a little cooler if you have a blimp. And a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1233 times.
Similar Threads
call second constructor?
hashing problem
accepting user input into into an array
See if you can tackle this one
Help with toString message
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 08:45:38.