This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Constructor initialization results in Null Pointer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructor initialization results in Null Pointer" Watch "Constructor initialization results in Null Pointer" New topic
Author

Constructor initialization results in Null Pointer

John Davis
Ranch Hand

Joined: Nov 02, 2000
Posts: 189
I don't know why I get a Null Pointer for an object which I setup in my Constructor.
Here is the code which works:

Here is the code which results in a Null Pointer when I try to reference the object.

What's the idea here, and what am I doing wrong when I set the object up in the constructor?
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

It's not really a constructor if it returns something, so try removing the "void" return type.

HTH!
[ June 04, 2003: Message edited by: Steve Morrow ]
John Davis
Ranch Hand

Joined: Nov 02, 2000
Posts: 189
Steve,
Thanks a lot. I thought if I used void, that meant that it returned nothing. But I see that the compiler does not treat my "void" method as a constructor and therefore it never gets called, resulting in a null pointer.
Thanks again,
John Davis
There are 01 types of people in the world. Those who understand binary, and those who don't...
Rick Critchett
Greenhorn

Joined: Feb 20, 2003
Posts: 3
Sounds like a bit of a kooky compiler.
No error or warning and then odd constructor behavior for your syntactic mistake?
What compiler are you using?
-rick
John Davis
Ranch Hand

Joined: Nov 02, 2000
Posts: 189
Rick
I'm using javac from J2SE1.4.1_02, but I don't think its a kooky compiler. Based on another reply I received, apparently my "constructor" method wasn't really a constructor, and wasn't being called, because it had "void" in its footprint.
John Davis
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor initialization results in Null Pointer
 
Similar Threads
Making an object Immutable in java
Overloding Java Constructor
Stringbuffer remains unchanged in a method
Check the output!!
Saving to a txt file