Evening everyone.
I seem to have hit an odd problem when trying to pass an object to another objects constructor who’s constructor also relies on the object it’s being passed to.
For instance, take the example below:
ToolBar myToolBar = new ToolBar(webPanel);
WebPanel webPanel = new WebPanel(myToolBar);
However when constructing ToolBar it returns a NullPointerException. Ofcourse that’s because webPanel isn’t constructed yet and it requires it.
Both of the decelerations and initialization must remain in the same class (called BuildUI) since it’s where I set up the properties. (It also doesn’t make sense for a ToolBar to create a webPanel object and vice versa).
I’m also not sure if that’s even good programming practice, since both objects require references to each other.
Any advice here is much appreciated. Thanks, Tom.
This will lead to problems as you can see. Instead of this approach, you can use the setter-getter method approach where you construct the object with a default constructor such as
and then use the setter methods to set the required instance variables which are required for the object to be fully constructed.