0

I am trying to implement the NullObject design pattern on my class Node:

    class Node{
        Node nextNode;
        char key;
        Node prevNode;

        /* 
           Would like to initialize nextNode and prevNode to instance of 
           NullNode, something like this (I know what I am doing is wrong)
        */
        Node() {
            nextNode = new NullNode();
            prevNode = new NullNode();
        }
    }

    class NullNode extends Node {
         ....
    } 

With this code I get a StackOverflowError Exception. How can I tackle this issue?

TofuBeer
  • 60,850
  • 18
  • 118
  • 163
Sush
  • 25
  • 4

3 Answers3

1

You are getting a StackOverflow because the parent constructor is always called (see also: https://stackoverflow.com/a/527069/664108). In your case this results in endless recursion.

To avoid that, you will have to add a check in the Node constructor and call it explicitly from the NullNode constructor:

public class Node
{
    Node nextNode;
    char key;
    Node prevNode;

    Node() {
        Node(true);
    }
    Node(boolean createNullNodes) {
        if (createNullNodes) {
            nextNode = new NullNode();
            prevNode = new NullNode();
        }
    }
}

public class NullNode extends Node
{
    NullNode() {
        super(false);
    }
} 

A better solution for the NullObject pattern is using interfaces. This eliminates the constructor problem and also allows to remove the not needed nextNode and prevNode variables from the NullNode.

Example with interface:

public interface INode
{
    public char getKey();
    public INode getNext();
    public INode getPrev();
    // ...
}

public class Node implements INode
{
    Node nextNode;
    char key;
    Node prevNode;

    Node() {
        nextNode = new NullNode();
        prevNode = new NullNode();
    }
    public char getKey() {
        return key;
    }
    public INode getNext() {
        return nextNode;
    }
    public INode getPrev() {
        return prevNode;
    }
}

public class NullNode implements INode
{
    public char getKey() {
        return null;
    }
    public INode getNext() {
        return this;
    }
    public INode getPrev() {
        return this;
    }
} 
Community
  • 1
  • 1
Fabian Schmengler
  • 24,155
  • 9
  • 79
  • 111
  • Still getting stackoverflow..even after putting empty constructor in nullnode. – Sush Feb 14 '13 at 11:56
  • Sorry, I had a misconception there. Updated the answer with alternative solution. – Fabian Schmengler Feb 14 '13 at 12:29
  • I tried your above mentioned solution by Node() {Node(true); } and Node(boolean createNullNodes) {...} constructors..but getting an error at Node() {Node(true); } as the "The method Node(boolean) is undefined for the type Node" – Sush Feb 14 '13 at 17:37
0

Normally we do not reference the Subclass in the Suerclass, this somehow breaks the inheritance relation.

In your code there is something even worse that will cause a StackoverflowException because the superclass creates an object with the default constructor of the subclass which in turns calls the default constructor of the superclass and it will go infinitely until your program crashes.

You can see an implementation of the Null Object Pattern here

iTech
  • 18,192
  • 4
  • 57
  • 80
0

Try this

    public clas Node
    {
        Node nextNode;
    char key;
    Node prevNode;

    Node() {
        this(true);
    }
    Node(boolean createNullNodes) {
        if (createNullNodes) {
            nextNode = new NullNode();
            prevNode = new NullNode();
        }

    }
}

    public class NullNode extends Node
    {
        NullNode() {
            super(false);
        }
    } 

To call one constructor from another constructor use this(args)... you cannot call it directly

prashant
  • 1,382
  • 1
  • 13
  • 19