• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

A little help with a null pointer exception

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much that helped alot!
reply
    Bookmark Topic Watch Topic
  • New Topic