• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointerException

 
tyler naughton
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and as Rob said, you find the array exists, but the individual members are still null.
 
tyler naughton
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic