mardi 1 mars 2016

Null Pointer exception in decorator class

The Program was running fine before implementing null object pattren. But after that only BinarySearchTreeIterator is running fine , but CapitalizeDecorator is giving null pointer exception when being invoked in toString method()

import java.util.*;

public class BinaryTree<T>  implements  Collection {
private RootNode<T> root;
private int size;
private OrderStrategy orderStrategy;
private static LeafNode nullNode = new LeafNode<>();

/**
 * Node Classes
 * @param <T>
 */

static class BinaryNode<T> {

    protected T nodeData;
    protected BinaryNode<T> leftNode;
    protected BinaryNode<T> rightChild;

    BinaryNode() {
        nodeData      = null;
        leftNode  = nullNode;//*
        rightChild = nullNode;
    }

    BinaryNode(T t) {
        nodeData      = t;
        leftNode  = nullNode;
        rightChild = nullNode;
    }

    BinaryNode(T t, BinaryNode<T> l, BinaryNode<T> r) {
        nodeData      = t;
        leftNode  = l;
        rightChild = r;
    }
    BinaryNode<T> addNode(T newValue, OrderStrategy<T> o) {
        return o.addNode(newValue, this);
    }
}

static class RootNode<T> extends BinaryNode {
    RootNode(T t) {
        super(t);
    }
}

static class LeafNode<T> extends BinaryNode {
    private LeafNode() {
        nodeData      = null;
        leftNode  = null;
        rightChild = null;
    }

    @Override
    BinaryNode<T> addNode(Object newValue, OrderStrategy o) {
        return new BinaryNode(newValue);
    }
}
/**
 * BinarySearchTree Classes and Collection function implementation
 */

BinaryTree () {
    root = null;
    size = 0;
    orderStrategy = new NormalOrderer();
}

BinaryTree (OrderStrategy o) {
    root = null;
    size = 0;
    orderStrategy = o;
}

public boolean isEmpty() {
    return size == 0;
}


@Override
public boolean add(Object o) {
    //addNode((T)o);
      if (isEmpty()) {
          root = new RootNode<T>((T)o);
      }
      else {
          root.addNode((T)o, orderStrategy);
      }
      size++;
    return true;
}

@Override
public boolean addAll(Collection c) {      
    return false;
}

@Override
public void clear() {
    root = null;
    size = 0;
}

@Override
public boolean contains(Object o) {
    return false;
}

@Override
public int size() {
    return size;
}
/**
 * Iterator and Decorator Classes
 */

@Override
public BinarySearchTreeIterator<T> iterator() {
    return new BinarySearchTreeIterator<T>();
}

class BinarySearchTreeIterator<T> implements Iterator{
    Stack<BinaryNode<T>> encounteredNodes = new Stack<>();
    BinaryNode<T> currentNode;
    private BinarySearchTreeIterator() {
       // encounteredNodes = 

        currentNode = root;

        while (currentNode != null) {
            encounteredNodes.push(currentNode);
            currentNode = currentNode.leftNode;
        }
    }

   /* public BinaryNode<T> currentValue() {
        return encounteredNodes.peek();
    }*/
    public BinaryNode<T> next() {
        currentNode = encounteredNodes.pop();
        BinaryNode<T> result = currentNode;
          currentNode = currentNode.rightChild;
            while (currentNode != null) {
                encounteredNodes.push(currentNode);
                currentNode = currentNode.leftNode;
            }

        return result;
    }
    public boolean hasNext() {
        return (!encounteredNodes.isEmpty() ||currentNode != null);
    }



    @Override
    public void remove() {

    }
}

abstract class IteratorDecorator extends BinarySearchTreeIterator {
    IteratorDecorator() {}

    IteratorDecorator(BinarySearchTreeIterator bsti) {}
}

class CapitalizeDecorator extends IteratorDecorator {
    private BinarySearchTreeIterator<String> iterator;

  //  CapitalizeDecorator(){};

    CapitalizeDecorator(BinarySearchTreeIterator<String> i) {
        iterator = i;
    }

    @Override
    public BinaryNode next() {
        BinaryNode result = (BinaryNode)iterator.next();
        result.nodeData = ((String)result.nodeData).toUpperCase();
        return result;
    }

    @Override
    public boolean hasNext() {
        return iterator.hasNext();
    }

  /*  @Override
    public BinaryNode currentValue() {
        return iterator.currentValue();
    }*/
}

@Override
public Object[] toArray(Object[] a) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public String toString() {
     BinarySearchTreeIterator iterator = new CapitalizeDecorator((BinarySearchTreeIterator<String>) iterator());
    //BinarySearchTreeIterator iterator = new BinarySearchTreeIterator<>();
    String result = "";
    BinaryNode<T> n = null;
    int i = 0;
    while(iterator.hasNext()) {
        n = iterator.next();
        if(n.nodeData != null) {
            result += n.nodeData.toString()+"";
            i++;
        }
    }
    return result;

}


public static void main(String[] args) {
    BinaryTree<String> b = new BinaryTree();
    b.add("C");
    b.add("B");
    b.add("A");
    b.add("e");
    b.add("d");
    b.add("f");
    b.add("a");
    b.add("E");
    b.add("f");
    String s = b.toString();
    System.out.println(s.toString());

}

@Override
public boolean remove(Object o) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean containsAll(Collection c) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean removeAll(Collection c) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean retainAll(Collection c) {
    // TODO Auto-generated method stub
    return false;
}

}

The NormalOrderer.java is as follows:-

class NormalOrderer<T> extends OrderStrategy {
@Override
public BinaryTree.BinaryNode addNode(Object newValue, BinaryTree.BinaryNode currentNode) {
    if (((Comparable<Object>)newValue).compareTo(currentNode.nodeData) < 0) {
        currentNode.leftNode = currentNode.leftNode.addNode(newValue, this);
    }
    if (((Comparable<Object>)newValue).compareTo(currentNode.nodeData) >= 0) {
        currentNode.rightChild = currentNode.rightChild.addNode(newValue, this);
    }
    return currentNode;
}
}

Please help . I m unable to find the problem

Aucun commentaire:

Enregistrer un commentaire