Java 为什么这个按钮处理程序在第二次运行时返回null?
此按钮处理程序的目的是在二叉树中搜索随机访问文件中记录的位置。方法fillInfoField用于用返回的数据填充GUI。任何帮助都将不胜感激Java 为什么这个按钮处理程序在第二次运行时返回null?,java,nodes,binary-search-tree,Java,Nodes,Binary Search Tree,此按钮处理程序的目的是在二叉树中搜索随机访问文件中记录的位置。方法fillInfoField用于用返回的数据填充GUI。任何帮助都将不胜感激 private class HandlerSSN implements ActionListener { public void actionPerformed(ActionEvent event) { String ssnReqStr = tfReqSSN.getText(); String num;
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
我自己解决了这个问题。我需要重新实例化每个输入流。下面是我添加到按钮处理程序顶部的代码
try
{
fis = new FileInputStream("treeObject.dat");
ois = new ObjectInputStream(fis);
}
catch (IOException e)
{
e.printStackTrace();
}
你能显示节点类型的“左”和“右”对象的声明吗?@mehmetgüngör-我添加了它。
try
{
fis = new FileInputStream("treeObject.dat");
ois = new ObjectInputStream(fis);
}
catch (IOException e)
{
e.printStackTrace();
}