Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointerException - could somebody explain why ?

 
roni suvanto
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
first time this year with Java and seems like I've forgotten some basic concepts about OOP
I'm coding a simple ascii-based flat-file database as a homework and I didn't get very far before first problems appeared.
Exception in thread "main" java.lang.NullPointerException
at Yearclass.ask(Register.java:103)
at Register.main(Register.java:7)
Could somebody be kind enough to tell me, what's wrong with the following code ?
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi roni
in YearClass's Students[] u did allocated memory for the array but not for the individual elements which are again objects of Student class.
so in the ask() method's for loop when you invoke method on students[i].ask() it gives u null pointer as students is allocated but not students[i].
somewhere u shd have,
for(int i=0; i < MAX; i++)
students[i] = new Student();
u know..
regards
maulin
 
roni suvanto
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maulin Vasavada:
hi roni
so in the ask() method's for loop when you invoke method on students[i].ask() it gives u null pointer as students is allocated but not students[i].

Thank you Maulin. It works fine now. Looks like I should brush up with some basic OOP concepts.
- Roni
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic