aspose file tools*
The moose likes Beginning Java and the fly likes NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NullPointerException " Watch "NullPointerException " New topic
Author

NullPointerException

tyler naughton
Greenhorn

Joined: Mar 15, 2011
Posts: 3
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

Joined: Oct 27, 2005
Posts: 19541
    
  16

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
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

Joined: Oct 13, 2005
Posts: 36453
    
  15
. . . and as Rob said, you find the array exists, but the individual members are still null.
tyler naughton
Greenhorn

Joined: Mar 15, 2011
Posts: 3
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

Joined: Oct 13, 2005
Posts: 36453
    
  15
You're welcome
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: NullPointerException
 
Similar Threads
See if you can tackle this one
hashing problem
call second constructor?
accepting user input into into an array
Help with toString message