无论我尝试使用框架类中的行bTree.binaryTree[i].Data=in.nextLine();
做什么,都会导致
输入二进制树中的节点数: 5 例外 线程"主要" java.lang.NullPointerException节点#0的详细信息 binaryTree.framework.intialize(framework.java:19)输入Data:at binaryTree.main.main(main.java:9)进程已完成退出代码1
Github要点:https://gist.github.com/D-codex/0051d59abb91fafc73fbec0fa7dad356
已编辑:https://gist.github.com/D-codex/009a5afa391c760b7ecc88f8109662c0 (遵循GhostCat的建议)
我已尽最大努力初始化几乎所有内容,以避免出现“无效”问题。任何地方,但这个例外不断出现
节点类
package binaryTree;
import java.util.*;
public class node {
boolean hasLeft,hasRight;
boolean isRootPrimary,isRoot;
String Data;
String nodeID,leftNodeID,rightNodeID,parentID;
int childrenNodeCount;
node(){
hasLeft=false;
hasRight=false;
Data="";
childrenNodeCount=0;
}
void check(){
if(hasLeft||hasRight)
isRoot=true;
}
String fetchRoot(){
return parentID;
}
String fetchID(){
return nodeID;
}
String fetchSiblingID(){
return "("+String.valueOf(leftNodeID)+" "+String.valueOf(rightNodeID)+")";
}
}
树类
package binaryTree;
import java.util.*;
public class Tree extends node{
node binaryTree[];
int count;int rootID,height,depth;
String siblingPairs,leaves;
Tree(int count){
binaryTree=new node[count];
this.count=count;
rootID=0;
height=0;depth=0;
}
void finalise(){
for(int i=0;i<count;i++){
if(binaryTree[i].childrenNodeCount==0)
leaves=String.valueOf(binaryTree[i].fetchID());
if(binaryTree[i].childrenNodeCount==2){
siblingPairs=binaryTree[i].fetchSiblingID();
}
}
StringTokenizer st=new StringTokenizer(leaves);
while(st.hasMoreElements()){
int counter=0;
int leafID=Integer.valueOf(st.nextToken());
node temp=binaryTree[leafID];
while(temp.isRootPrimary){
temp=binaryTree[Integer.valueOf(temp.fetchRoot())];
counter=counter+1;
}
if(counter>height) {
height = counter;
depth=height-1;
}
}
}
}
框架类
package binaryTree;
import java.util.*;
public class framework {
public Tree intialize(){
System.out.print("Enter the Number of Nodes in the Binary Tree :");
Scanner in=new Scanner(System.in);
int count=in.nextInt();
Tree bTree=new Tree(count);
int ID=0;int i=0;
while(i<count){
System.out.println("Details for node#"+i);
System.out.print("Enter Data :");
bTree.binaryTree[i].Data=in.nextLine();
bTree.binaryTree[i].nodeID=String.valueOf(ID);
if(i!=0){
boolean flag = true;
while (flag) {
String parentID="";
System.out.print("Enter the node's Parent:");
parentID = in.nextLine();
if (bTree.binaryTree[Integer.valueOf(parentID)].childrenNodeCount >= 2) {
System.out.println("Parent Node is Full");
}
else {
bTree.binaryTree[i].parentID = in.nextLine();
bTree.binaryTree[Integer.valueOf(parentID)].childrenNodeCount=bTree.binaryTree[Integer.valueOf(parentID)].childrenNodeCount+1;
flag=false;
}
}
System.out.print("Is the node Parent's Left?");
String temp=in.next();
if(temp.equalsIgnoreCase("yes")||temp=="1"||temp.equalsIgnoreCase("y")){
bTree.binaryTree[Integer.valueOf(bTree.binaryTree[i].parentID)].hasLeft=true;
bTree.binaryTree[Integer.valueOf(bTree.binaryTree[i].parentID)].leftNodeID=bTree.binaryTree[i].nodeID;
}
else {
bTree.binaryTree[Integer.valueOf(bTree.binaryTree[i].parentID)].hasRight = true;
bTree.binaryTree[Integer.valueOf(bTree.binaryTree[i].parentID)].rightNodeID=bTree.binaryTree[i].nodeID;
}
}
else
bTree.binaryTree[i].isRootPrimary=true;
bTree.binaryTree[i].check();
i=i+1;ID=ID+1;
}
bTree.finalise();
return bTree;
}
public void displayAll(Tree bTree){
System.out.println("Running a Full Analysis on the Binary Tree");
System.out.println("Printing nodes Details");
for(int i=0;i<bTree.count;i++){
System.out.println("Node #"+i);
System.out.println("PrimaryRoot :"+bTree.binaryTree[i].isRootPrimary);
System.out.println("isRoot :"+bTree.binaryTree[i].isRoot);
System.out.println("Data :"+bTree.binaryTree[i].Data);
System.out.println("Number of Children Nodes :"+bTree.binaryTree[i].childrenNodeCount);
System.out.println("hasLeft :"+bTree.binaryTree[i].hasLeft);
if(bTree.binaryTree[i].hasLeft)
System.out.println("LeftNodeID :"+bTree.binaryTree[i].leftNodeID);
System.out.println("hasRight :"+bTree.binaryTree[i].hasRight);
if(bTree.binaryTree[i].hasRight)
System.out.println("RightNodeID :"+bTree.binaryTree[i].rightNodeID);
System.out.println("parentID :"+bTree.binaryTree[i].fetchRoot());
System.out.println("SiblingPairs (if any) :"+bTree.binaryTree[i].fetchSiblingID());
System.out.println("____________________________________________________________________________");
}
System.out.println("Tree Specifications");
System.out.println("Total No. of Nodes :"+bTree.count);
System.out.println("Height of Tree :"+bTree.height);
System.out.println("Depth of Tree :"+bTree.depth);
System.out.println("Leaves :"+bTree.leaves);
System.out.println("Sibling Pairs :"+bTree.siblingPairs);
}
}
主要课程
package binaryTree;
public class main {
public static void main(String Args[]){
framework Framework=new framework();
Tree binaryTree=Framework.intialize();
Framework.displayAll(binaryTree);
}
}
答案 0 :(得分:1)
错误发生在您访问树对象bTree.binaryTree[i].data
的位置,您正在尝试访问null属性。
System.out.println("Details for node#"+i);
System.out.print("Enter Data :");
//bTree.binaryTree[i] = new Tree(i); //uncomment this line its will work
bTree.binaryTree[i].Data=in.nextLine(); bTree.binaryTree[i] is null or not initialized.
bTree.binaryTree[i].nodeID=String.valueOf(ID);
在这种情况下,您创建了一个对象数组,但是数组没有填充任何对象,访问这些位置将最终出现在NullPointerException中。使用new
运算符创建对象,然后分配到位置binaryTree[i]
,然后您就可以访问数据。
答案 1 :(得分:1)
你的问题在这里:
binaryTree=new node[count];
这只会创建新但空数组。
首先必须迭代该数组并在每个插槽中插入一个Node对象!
此外:您想阅读有关Java编码样式指南的内容。例如,类名始终以大写字母开头。