File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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 Murach's Java Servlets and JSP this week in the Servlets 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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointerException - could somebody explain why ?
 
Similar Threads
Jdbc Prepared Statement execute()
Passing Objects Created By User to Array in Different Class
Array returning last value entered
Which way is efficient, declaring a function as static or creating an object?
help solving dynamic method invocation exception.