我一直试图弄清楚为什么我的程序会崩溃。当我在eclipse中编译时,一切都顺利进行,直到我选择"搜索"菜单驱动BST中的选项。我不断收到错误java.lang.NullPointerExcpetion。部分我感觉好像我的add方法在创建具有指定值的新节点时工作不正常,因此返回空值但是我不确定。有人会介意看看我的代码,看看我哪里出错了吗?谢谢! 这是我的堆栈跟踪:
线程中的异常" main"显示java.lang.NullPointerException
在BTree.BTSearch(BTree.java:55)
在BTUser.ProcessChoice(BTUser.java:97)
BTUser.main上的(BTUser.java:26)
public class BTree
{ // opens BTree
// att
BTNode Root;
// code
public BTNode GetRoot()
{ // open GRo
return(Root);
} // closes GRo
public void SetRoot(BTNode proot)
{ // opens SRo
Root=proot;
return;
} // closes SRo
public void DisplayAll(BTNode current)
{ // opens Disp
if(current!=null)
{ // opens if
DisplayAll(current.GetLeft());
System.out.println(""+current.GetName());
DisplayAll(current.GetRight());
} // closes if
return;
} // closes disp
public BTNode BTSearch(BTNode proot, String pName)
{ // open BTSearch
// locals
BTNode found;
// code
if(proot==null)
{ //opens if
found=null;
} // closes if
else if(pName.equalsIgnoreCase(proot.GetName()))
{ // opens if
found=proot;
} //closes if
else if(pName.compareToIgnoreCase(proot.GetName())<=0)
{ // opens if
found=BTSearch(proot.GetLeft(),pName);
} // closes if
else
{ // opens 3rd else
found=BTSearch(proot.GetRight(),pName);
} // closes 3rd Else
return(found);
} // closes BTSearch
BTNode AddOne(BTNode proot, String pName)
{ // opens add
//locals
BTNode Temp;
Temp = new BTNode(pName);
// code
if(proot==null)
{ // opens if
proot=Temp;
Temp.SetName(pName);
} // closes if
else if(pName.compareToIgnoreCase(proot.GetName())<=0)
{ // opens if
proot.SetLeft(AddOne(proot.GetLeft(),pName));
} // closes if
else if(pName.compareToIgnoreCase(proot.GetName())>=0)
{ // opens if
proot.SetRight(AddOne(proot.GetRight(),pName));
} // opens if
// } // closes 2nd else
return(proot);
} // closes AddOne
} // closes BTree
public class BTNode
{ // open BTN
// att
String Name;
BTNode LPointer;
BTNode RPointer;
// code
public BTNode(String pName)
{ // open constructor
Name=pName;
return;
} // close constuctor
public String GetName()
{ // opens GN
return(Name);
} // closes GN
public void SetName(String pName)
{ // open SetName
Name = pName;
return;
} // closes set name
public BTNode GetLeft()
{ // opens GL
return(LPointer);
}// closes GL
public void SetLeft(BTNode pLeft)
{ // opens SL
LPointer=pLeft;
return;
} // closes SL
public BTNode GetRight()
{ // opens GR
return(RPointer);
} // closes GR
public void SetRight(BTNode pRight)
{ // opens SR
RPointer=pRight;
return;
} // close SR
}
import java.util.Scanner;
public class BTUser
{ // opens BTUser
// code
public static void main (String args[])
{ // opens main
// locals
int Choice;
BTree MyTree;
MyTree = new BTree(); // instantiate
do
{ // open post test
DispMenu();
Choice=GetChoice();
ProcessChoice(Choice, MyTree);
} // close post test
while(Choice!= 4);
// methods
}
public static void DispMenu()
{ // opens menu
System.out.println(" Please Make a Selection ");
System.out.println("\n");
System.out.println(" 1. Add Name ");
System.out.println(" 2. Search a Name ");
System.out.println(" 3. Display All Names ");
System.out.println(" 4. Quit ");
} // closes menu
public static int GetChoice()
{ // opens getChoice
// locals
int lChoice=-1;
try{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter your Choice ");
lChoice=keyboard.nextInt();
}catch(Exception e){
System.out.println("invalid choice");
}
return(lChoice);
} // closes get Choice
public static String GetName()
{ // opens get name
// locals
String prompt;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a name");
prompt=keyboard.nextLine();
return(prompt);
} // closes get name
public static void ProcessChoice(int Choice, BTree MyTree)
{ // opens process choice
// locals
String Prompt=null;
switch (Choice)
{ //open switch
case 1:Prompt=GetName(); MyTree.SetRoot(MyTree.AddOne(MyTree.GetRoot(),Prompt));
break;
case 2: MyTree.BTSearch(MyTree.GetRoot(),Prompt);
break;
case 3: MyTree.DisplayAll(MyTree.GetRoot());
break;
case 4: System.out.println("\n Have a nice day");
break;
default: System.out.println("\n Invalid Choice");
break;
}
} // closes process choice
}
答案 0 :(得分:0)
此问题是由以下代码行引起的:
else if(pName.equalsIgnoreCase(proot.GetName()))
原样,pName始终为null。因此,当在空引用上调用pName.equalsIgnoreCase()
时,会得到空指针异常。追溯问题,我发现错误在于你的switch语句:
switch (Choice)
{ //open switch
case 1:Prompt=GetName(); MyTree.SetRoot(MyTree.AddOne(MyTree.GetRoot(),Prompt));
break;
case 2: MyTree.BTSearch(MyTree.GetRoot(),Prompt);
break;
case 3: MyTree.DisplayAll(MyTree.GetRoot());
break;
case 4: System.out.println("\n Have a nice day");
break;
default: System.out.println("\n Invalid Choice");
break;
}
您忘记为案例2添加Prompt=GetName();
行,因此在选择案例2(搜索名称)时,pName永远不会设置且始终为null。