• Post Reply Bookmark Topic Watch Topic
  • New Topic

A little help with a null pointer exception  RSS feed

 
Tim Mannion
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fairly new to java here....teaching myself through a text book...I asked a couple questions on here before and found it real helpful. hope someone can tell me what I'm doing wrong.
Basically I'm supposed to create an application that creates an array of 10 Student objects each of which have 5 Class objects then display the grades. It compiles fine but once I add the first student array terminal says "Exception in thread "main" java.lang.NullPointerException
at InputGrades.main(InputGrades.java:14)"
can anyone explain my mistake to me?


i didn't include my displayResults method to save room/didnt think it mattered.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error message tells us that the NPE is coming here:

Looking at that, we can see that the only possibilities are that student is null or student[x] is null. Looking at the code preceding that line, we can see two things:

1) We did student = new Student[10], so student is not null.

2) We never did student[whatever] = new Student(), so student[x] is null.

(When we create an array, such as with new Student[10], that only creates the array itself, not any of the objects its elements refer to. All the student[whatever] elements are null until we initialize them.)
 
Tim Mannion
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much that helped alot!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!