【问题标题】:NullPointerException for BinaryTree created through ObjectInputStream通过 ObjectInputStream 创建的 BinaryTree 的 NullPointerException
【发布时间】:2015-04-11 18:11:51
【问题描述】:

我正在使用 ObjectInputStream 从二进制文件中保存的数据(不是节点)构建二叉树。但是,似乎当控件返回到驱动程序时,作为结果创建的树对象为空,我看到了 NullPointerException。调用testTree 中的第一个方法getHeight() 时发生异常。当我尝试遍历新建的树对象时,它返回空白。我不明白为什么。有什么建议?

这是我试图保持简短的相关代码:

public static void main(String [] args)
{
....
BST<Integer> T = new BST<Integer>();
T.buildTree("BST.dat");
testTree(T);
...
}

public class BST<T extends Comparable<? super T>>
         extends CBT<T> 
         implements STInterface<T> 
{
 private BN<T> root; 

 public BST()
 {
  super();
 }
//other methods here.

 }
 public class CBT<T extends Comparable<? super T>> extends BT<T> implements CTInterface<T>

{ 
   public ComparableBinaryTree()
    {
     super();
    }
}

public class BT<T> implements BTInterface<T>
{
  private BN<T> root;
  public int getHeight()
{
  return root.getHeight();
}

 ...

 public void buildTree(String fileName)
 {
  try
 {
  ObjectInputStream IS = new ObjectInputStream(
    new FileInputStream(fileName));
  double numberOfNodes = (double)IS.readInt();
  BN<T> root = new BN<>();
  RecReadTree(IS, numberOfNodes, root);
  IS.close();
 }
 catch (IOException e)
 {
  System.out.println("Reading problem");
 }
}
 ....
 }

  private void RecReadTree(ObjectInputStream IS, double numberOfNodes, BN<T> node)
  {
if (node != null)
  {
  try
  {
      if (numberOfNodes == 1)
      {
        @SuppressWarnings("unchecked")
        T data = (T) IS.readObject();
        node.setData(data);
        return;
      }

      double left = Math.ceil((numberOfNodes - 1)/2.0); //ceiling
      double right = Math.floor((numberOfNodes - 1)/2.0); //floor

      BN<T> leftChild = new BN<>();
      node.setLeftChild(leftChild);
      RecReadTree(IS, left, leftChild);
      @SuppressWarnings("unchecked")
      T data = (T) IS.readUnshared();
      node.setData(data);
      if (right > 0)
      {
        //new node
        BN<T> rightChild = new BN<>();
        node.setRightChild(rightChild);
        RecReadTree(IS, right, rightChild);
      }
  }
  catch (Exception e)
  {
    System.out.println("Rec Reading Problem" + e);
  }
 }
 }
}
}   

public class BN<T> 
  {
    public int getHeight()
  {
    return getHeight(this); 
  } // end getHeight

private int getHeight(BN<T> node)
 {
  int height = 0;

  if (node != null)
      height = 1 + Math.max(getHeight(node.getLeftChild()),
                             getHeight(node.getRightChild()));                      
  return height;
 }
}

编辑:我在构建二叉树时添加了非空条件。它仍然会导致错误。这似乎与范围和继承有关,因为如果需要,我可以从 RecReadTree 内部访问 getHeight() 方法没问题。

【问题讨论】:

标签: java nullpointerexception objectinputstream


【解决方案1】:

我看到的方式是您试图从子类访问父函数,但由于未初始化,父函数在其中使用的值可能为 null。这有很多可能性,但这只是继承时显而易见的可能性之一。干杯!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    相关资源
    最近更新 更多