可迭代链接位置列表中的Java NullPointerException

时间:2014-10-15 19:17:42

标签: java list nullpointerexception tree iterable

我正在尝试编写一个使用二叉树来实现的通用树实现,并且我遇到了返回给定节点的所有子节点的子方法的问题。如果您尝试调用out of bounds节点,则二叉树会抛出错误,这就是此函数工作的原因。

/** Returns an iterable collection of the children of a given node. */
public Iterable<Position<E>> children(Position<E> v) throws IllegalArgumentException {
   PositionalList<Position<E>> children = new LinkedPositionalList<Position<E>>();
   try {
       Position<E> current = bTree.left(v);
       children.addFirst(current);
       Position<E> child;
       while (true){
           child = bTree.right(current);
           children.addLast(child);
           current = child;
                    }
       }

catch (IllegalArgumentException e){
       return children;
   }
  }

在测试期间,它返回此行的空指针异常,elementTemp = iteratorTemp.next(。getElement();,在返回B的所有子节点后返回此循环。

// test of children of B
 Position<String> r = gTree.root();
 String elementTemp;
 System.out.print("Children of " + r.getElement()+": ");
 Iterator<Position<String>> iteratorTemp= gTree.children(r).iterator();


 while (iteratorTemp.hasNext()){
    elementTemp= iteratorTemp.next().getElement();
    System.out.print(elementTemp +" ");
 }

这是输出

B的孩子:线程“main”java.lang.NullPointerException中的D I异常     在assignment2.Test1.main(Test1.java:79)

编辑以添加完整的test1类

import java.util.Iterator;

class Test1 {


public static void main(String [] args){

GeneralTreeBBT<String> gTree=new GeneralTreeBBT();
gTree.addRoot("B");
Position<String> r5=gTree.insertChild(gTree.root(), "A");
Position<String> r1=gTree.insertChild(gTree.root(), "D");
Position<String> r2=gTree.insertChild(gTree.root(), "I");
gTree.insertChild(r1, "C");
Position<String> r4= gTree.insertChild(r1, "E");
r1=gTree.insertChild(r1, "G");
Position<String> r3=gTree.insertChild(r1, "F");
gTree.insertChild(r2, "H");
gTree.insertChild(r2, "L");

// test of parent of B
 System.out.print("Parent of  B: ");
 System.out.println (gTree.parent(gTree.root()).getElement());
 System.out.println();

 // test of parent of G
 System.out.print("Parent of  G: ");
 System.out.println (gTree.parent(r1).getElement());
 System.out.println();

 // test of parent of I
 System.out.print("Parent of  I: ");
 System.out.println (gTree.parent(r2).getElement());
 System.out.println();

 // test of parent of F
 System.out.print("Parent of  F: ");
 System.out.println (gTree.parent(r3).getElement());
 System.out.println();

 // test of children of B
 Position<String> r = gTree.root();
 String elementTemp;
 System.out.print("Children of " + r.getElement()+": ");
 Iterator<Position<String>> iteratorTemp= gTree.children(r).iterator();


 while (iteratorTemp.hasNext()){
    elementTemp= iteratorTemp.next().getElement();
    System.out.print(elementTemp +" ");
 }
 System.out.println();
 System.out.println();

// test of number of children of B
r = gTree.root();
System.out.print("Number of children of " + r.getElement()+": ");
System.out.println(gTree.numChildren(r));
System.out.println();

// test of children of I

System.out.print("Children of " + r2.getElement()+": ");
iteratorTemp= gTree.children(r2).iterator();


while (iteratorTemp.hasNext()){
    elementTemp= iteratorTemp.next().getElement();
    System.out.print(elementTemp +" ");
}

System.out.println();
System.out.println();

// test of number of children of I

System.out.print("Number of children of " + r2.getElement()+": ");
System.out.println(gTree.numChildren(r2));
System.out.println();

//测试是外部G

System.out.print("G is external: ");
if (gTree.isExternal(r1))
        System.out.println ("YES");
else System.out.println ("NO");
System.out.println();

//测试是内部的我

 System.out.print("I is internal: ");
 if (gTree.isInternal(r2))
        System.out.println ("YES");
 else System.out.println ("NO");
 System.out.println();

 // test of is external F

System.out.print("F is external: ");
if (gTree.isExternal(r3))
        System.out.println ("YES");
else System.out.println ("NO");
System.out.println();

//内部E的测试

 System.out.print("E is internal: ");
 if (gTree.isInternal(r4))
        System.out.println ("YES");
 else System.out.println ("NO");
 System.out.println();

}

}

0 个答案:

没有答案