-2

Anyway, my code looks like this:

        SystemOutput systemOutput = null;
        SystemCL system = null;
        WindowsForm wf = null;

        wf = new WindowsForm(system);
        systemOutput = new SystemOutput(wf);
        system = new SystemCL(systemOutput, wf);

The rest of the code doesn't really matter for the matter of solving my problem (I think?)

So as you can see the objects reference each other, which means that if the other objects hasn't been initialized yet, it will give me an error. So I tried first making them null, but now the first object get a nullreferencepointer, because the object is null.

How do I fix this puzzle? Is there a better way to do this?

Note What is a NullReferenceException, and how do I fix it? does not cover this case.

Community
  • 1
  • 1
  • what you are trying to do your object initialization is nested with each other ? – Mostafiz May 11 '16 at 16:33
  • I am not sure what that means, not really atleast. Ain't a nested class a class that's kind of made inside another class? If so, then no – user2972258 May 11 '16 at 16:41
  • Assigning objects as `null` does not initialize them. Why is it a surprise to you that you are getting a `NullReferenceException` when you are deliberately setting `null`? – AntiTcb May 11 '16 at 16:42
  • Wasn't a surprise that I got NullReferenceException. The question was how to avoid them, as Alexei Levenkov answered. – user2972258 May 11 '16 at 16:49

2 Answers2

1

The answer is to not create the puzzle in the first place. Don't create circular references, like where WindowsForm depends on SystemCL, SystemOutputdepends on WindowsForm, and then SystemCL depends on both of them.

Don't think of it as a puzzle in terms of how to make these classes work as they were designed. The puzzle is how to design them differently so that they don't depend on each other that way.

That's as detailed as I can get without knowing what the classes actually do.

Most of the complexity in object oriented programming comes determining how classes will depend on other classes. Or if you're working on someone else's code, it's figuring out which classes depend on which other classes. Getting this right is IMO one of the most important aspects of OOP.

I think you're on the right track by passing objects into the constructors of other objects when one class depends on another. That's a good practice. The next step is to figure out what belongs in each class so that they don't need to depend on each other circularly.

Scott Hannen
  • 27,588
  • 3
  • 45
  • 62
0

First - it is better to redesign your classes to avoid circular references. See https://softwareengineering.stackexchange.com/questions/11856/whats-wrong-with-circular-references for discussion.

If you must have such references (i.e. building tree with parent/child relationships) than one of the objects have to provide a way to delay setting one of the relationship references.

Most straightforward - expose property and set it later after other objects are constructed:

    wf = new WindowsForm(/*nothing*/);
    systemOutput = new SystemOutput(wf);
    system = new SystemCL(systemOutput, wf);
    wf.SetSystem(system); // method to make it less tempting to set later

There are also multiple ways to make it safer (factories, constructing object when adding,..) including even immutable trees.

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179