1
我正在做一項涉及使用二叉樹的任務,但是我們給出的示例程序無法正常工作(很高興知道這個小程序在提供給易感人的頭腦之前已經過測試)。它在「info.name = input.readLine(」Enter student ID:「);」「行上拋出一個NullPointerException控制檯行拋出NullPointerException?
import java.io.*;
class BinaryTree1
{
public static void main(String[] args) throws IOException
{
Console input = System.console();
String line = new String();
Student info;
info = new Student();
Student root;
root=null;
System.out.println("Input student name followed by mark 10 times;");
for(int i = 1; i < 10; i++)
{
info.name= input.readLine("Enter student ID: ");
line = input.readLine();
info.id = Integer.parseInt(line);
root=addNode(root, info);
}
}
static Student addNode(Student root, Student info)
{
if(root == null)
{
root= new Student();
root.left = null;
root.right = null;
root.name = info.name;
root.id = info.id;
}
else
{
if(info.id < root.id)
root.left = addNode(root.left, info);
else //(info.id > root.id)
root.right = addNode(root.right, info);
}
return root;
}
}