为什么这个按钮处理程序在第二次运行时返回null?

时间:2013-12-01 23:48:16

标签: java nodes binary-search-tree

此按钮处理程序的目的是在二叉树中搜索随机访问文件中记录的位置。 fillInfoField方法用于使用返回的数据填充GUI。任何帮助将不胜感激!

    private class HandlerSSN implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        String ssnReqStr = tfReqSSN.getText();
        String num;
        int ssn;

        BalanceOnDemand.Node currNode = null;
        BalanceOnDemand myTree = new BalanceOnDemand();

        if (ssnReqStr.length() == 0) {
            tfMsg.setText("Lookup by Name (partial match allowed)");
            tfReqName.requestFocus();
            return;
        } else {
            try {
                raf.seek(0);
                myTree.root = (BalanceOnDemand.Node) ois.readObject();

                num = ssnReqStr.replaceAll("[^0-9]", "");
                ssn = Integer.parseInt(num);
                currNode = myTree.find(ssn);
                System.out.println(currNode);
                if(currNode != null){
                    raf.seek(currNode.loc - REC_LEN);
                    fillInfoFields(readCurrRec());
                }else{
                    System.out.println("Test");
                    tfMsg.setText("SSN \"" + tfReqSSN.getText() + "\" was not found");
                    return;
                }

            } catch (IOException | ClassNotFoundException e) {
                System.out.println(currNode.id);
                tfMsg.setText("SSN \"" + tfReqSSN.getText()
                        + "\" was not found");
            }
        }

    }
}

如果你想看到它,这是find方法。

public Node find(int key)
{
Node current;
current = root;

while(current!=null && current.id!=key)
  {
    if(key<current.id){
      current = current.left;
    }else{
      current = current.right;
    }
  }
  return current;

}

  class Node implements Serializable

{

private static final long serialVersionUID = 1L;
public int    id;
public int    loc;
public Node   left;
public Node   right;

    public Node(int i,int i2)
    {
      id    = i;
      loc  = i2;
      left  = null;
      right = null;
    }
  }

1 个答案:

答案 0 :(得分:0)

我自己解决了这个问题。我需要重新实例化每个输入流。这是我添加到按钮处理程序顶部的代码。

        try
        {
          fis = new FileInputStream("treeObject.dat");
          ois = new ObjectInputStream(fis);
        }
        catch (IOException e)
        {
          e.printStackTrace();
        }
相关问题