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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NullPointerException - could somebody explain why ? " Watch "NullPointerException - could somebody explain why ? " New topic
Author

NullPointerException - could somebody explain why ?

roni suvanto
Greenhorn

Joined: Jul 23, 2002
Posts: 13
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

Joined: Nov 04, 2001
Posts: 1871
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

Joined: Jul 23, 2002
Posts: 13
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: NullPointerException - could somebody explain why ?