开发者

Java LinkedTree Help

I am trying to implement a linked tree but I am getting a nullpointer exception when I run it. Can you help me fix it up.

    Exception in thread "main" java.lang.NullPointerException
        at assign2Trees.LinkedTree.size(LinkedTree.java:16)
        at assign2Trees.LinkedTree.main(LinkedTree.java:80)
import java.util.Iterator;
import java.util.NoSuchElementException;

public class LinkedTree<E> implements Tree<E>{

    private Node<E> root;

    public LinkedTree()
    {
        super();
    }
    public int size()
    {
        return root.getChildren().size() + 1;
    }   
    public boolean isEmpty() 
    {
        if(root == null)
            return true;
        else
            return false;
    }
    public Iterator<E> iterator() 
    {
        return new Iterator<E>()
        {
            private int i = 0;

            public boolean hasNext()
            {
                return i < LinkedTree.this.size();
            }
            public E next() throws NoSuchElementException
            {
               if (!hasNext()) 
               {
                   return null;
开发者_运维问答               }
               return (E) LinkedTree.this.root.getChildren().get(i++);
            }
            public void remove() 
            {

            }
        };
    }
    public E replace(E oldE, E newE) 
    {
        int i = root.getChildren().indexOf(oldE);
        root.getChildren().add(i, (Node<E>) newE);  
        return newE;
    }
    public E root() throws EmptyTreeException 
    {
        if (isEmpty())
            throw new EmptyTreeException ();
        return this.root();
    }
    public E parent(E node) 
    {
        return (E) ((Node<E>) root.getChildren()).getParent((root.getChildren().get(root.getChildren().indexOf(node))));    
    }
    public Iterable<E> children(E node) 
    {
        return (Iterable<E>) root.getChildren().listIterator();
    }
    public void delete(E node) throws RootNonDeletableException 
    {
        if (node == null)
            throw new RootNonDeletableException();
        else
            root.getChildren().remove(root.getChildren().indexOf(node));
    }

    public static void main(String[] args)
    {
        LinkedTree<Node<String>> tree = new LinkedTree<Node<String>>();
        System.out.println(tree.size());

    }


}


import java.util.ArrayList;
import java.util.List;

public class Node<E>{

    protected E contents;
    protected List<Node<E>> children;
    protected Node<E> parent;

    public Node()
    {
        super();
    }
    public Node(E contents)
    {
        this.setContents(contents);
    }
    public List<Node<E>> getChildren()
    {
        if(this.children == null)
        {
            return new ArrayList<Node<E>>();
        }
        return this.children;
    }
    public void setChildren(List<Node<E>> children)
    {
        this.children = children;
    }
    public void addChild(Node<E> child)
    {
        if (children == null)
        {
            children = new ArrayList<Node<E>>();
        }
        children.add(child);
    }
    public E getContents()
    {
        return this.contents;
    }
    public void setContents(E contents)
    {
        this.contents = contents;
    }
    public Node<E> getParent(Node<E> node)
    {
        return node.parent;
    }
    public void setParent(Node<E> parent, Node<E> child)
    {
        child.parent = parent;
    }
}


root is null. You're not intializing root to anything or checking that it's not null before accessing it on this line:

return root.getChildren().size() + 1;

Perhaps:

return (root == null) ? 0 : root.getChildren().size()+1;

So more than likely what you really ought to do is set root to a brand new node in your constructor, say:

public LinkedTree()
{
    super();
    root = new Node<E>();
}

You also have an error with your root() function:

public E root() throws Exception 
{
    if (isEmpty())
        throw new Exception ();
    return this.root();
}

Will call itself. Perhaps you meant

public E root() throws Exception 
{
    if (isEmpty())
        throw new Exception ();
    return this.root;
}

Now to start building your tree in your main method you can do something like:

tree.root().addChild(new Node<String>("test"));

What this does is get the root of the tree, then adds a child to it.

Supposing you want to add a complicated structure, say, a node with a child, you could do:

Node<String> deepest = new Node<String>("deepest node");
Node<String> deeper  = new Node<String>("deeper node");
deeper.addChild(deepest);
tree.root().addChild(deeper);

This gives you a tree with structure:

[root] +- [deeper] +- deepest


Change

public List<Node<E>> getChildren()
{
    if(this.children == null)
    {
        return new ArrayList<Node<E>>();
    }
    return this.children;
}

to

public List<Node<E>> getChildren()
{
    if(this.children == null)
    {
        return this.children = new ArrayList<Node<E>>(); // set the this.children field and return.
    }
    return this.children;
}

Tell me how that goes.


The root node is never initialized, so you're trying to call getChildren() on an object that doesn't exist.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜