Weird/stupid error message...

I have a WorldObject class, with a LandscapeObject which extends it, and then a LandscapeBox which extends that. WorldObject has a basic constructor (no args) and so does LandscapeObject. There are also several methods/variables that are inherited along the way.

However, when i try and call:

LandscapeBox tempObject;

tempObject = new LandscapeBox(x,y,z, R,G,B, xPos,yPos,zPos);

The console displays a null pointer exception after the second line. Despite the fact that the relavent constructor exists in LandscapeBox. It doesn't even get to the first line of the constructor (a System.out.println statement).

Is there something wrong with my inheritance? Or am i missing something basic after staring at my code for too long?

[793 byte] By [OrangyTang] at [2007-9-26 13:02:38]
# 1

As a guess, I would say you are calling an object member function from a constructor, or accessing an unitialised member variable from one of the constructors of one of your base classes. Without seeing the code it is hard to tell, but you could try putting System.out messages in all the constructors and see what that tells you

rob_canoe2 at 2007-7-2 12:54:25 > top of Java-index,Archived Forums,Java Programming...
# 2
Yep; try putting the debug line in the constructor of WorldObject and LandscapeObject - thet will be called before the constructor of LandscapeBox.The error messages usually say the class and the line number (if available) the exception was met - doesn't it do now?
jsalonen at 2007-7-2 12:54:25 > top of Java-index,Archived Forums,Java Programming...
# 3

> As a guess, I would say you are calling an object

> member function from a constructor, or accessing an

> unitialised member variable from one of the

> constructors of one of your base classes. Without

> seeing the code it is hard to tell, but you could try

> putting System.out messages in all the constructors

> and see what that tells you

Well the first line in the LandscapeBox constructor that is called is a System.out.println, and thats not displayed :(

> Yep; try putting the debug line in the constructor of

> WorldObject and LandscapeObject - thet will be called

> before the constructor of LandscapeBox.

>

> The error messages usually say the class and the line

> number (if available) the exception was met - doesn't

> it do now?

For some odd reason the error message is just: "java.lang.NullPointerException". No line numbers or anything..

From what you've said it sounds like i've not understood how constructors are called. I had thought that only the constructor in the LandscapeBox would be called and nothing else. What constructors will be called from the base classes? And in what order?

OrangyTang at 2007-7-2 12:54:25 > top of Java-index,Archived Forums,Java Programming...
# 4

perhaps the best way to learn is to have a play yourself, create a really simple class, put a system.out message in the constructor, derive a new class from that one, put a system.out message in the constructor, derive a new class from that one, put s system.out message in the constructor.......

run it and all will become clear. (maybe)

rob_canoe2 at 2007-7-2 12:54:25 > top of Java-index,Archived Forums,Java Programming...
# 5

Got it sorted now :) Resorted to the tried and tested "bung system.out messages everywhere and trace the progress". Since i'd not expected the basic WorldObject and LandscapeObject constructors to be called they ended up setting a static pointer i needed to null... Odd how it didn't produce a line number though as it usually does.

But its working now and i've got my 3d cube properly displayed :) Thanks everybody.

OrangyTang at 2007-7-2 12:54:25 > top of Java-index,Archived Forums,Java Programming...
# 6

> What constructors will be called from the base classes? And in what order?

The defaults, unless specified otherwise using "super(...)". I recommend to have "super();" as the first line of your constructors (unless you want to pass the job to another constructor of the same class using this(...)), even though it's not necessary as the compiler will put it there for you -- just as a reminder.

Because of this recursive calling of constructors, the default constructor of java.lang.Object will be the first constructor that's really executed.

jsalonen at 2007-7-2 12:54:25 > top of Java-index,Archived Forums,Java Programming...