2016-12-23 73 views
0

無論我嘗試在無法解決顯示java.lang.NullPointerException

與框架類結果行bTree.binaryTree[i].Data=in.nextLine();做輸入節點的二叉樹數:異常在 線程「主「java.lang.NullPointerException節點#0的詳細信息,位於 binaryTree.framework.intialize(framework.java:19)輸入數據:at binaryTree.main.main(main.java:9)以退出代碼1結束的進程

Gith UB要點: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); 
} 
} 
+5

可能重複[什麼是NullPointerException,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-doi-i-fix -it) –

+0

遵循java命名約定,如果你不知道,請閱讀它。 –

+0

感謝您的快速接受。當我下週找到時間時,我會爲你提出一些建議。 – GhostCat

回答

1

你的問題是在這裏:

binaryTree=new node[count]; 

這只是創建了一個新的陣列

您首先必須迭代該數組並在每個插槽中插入一個Node對象!

此外:你想閱讀關於Java編碼風格指南。例如,類名始終以大寫字母開頭。

+0

現在嘗試你的建議我可以讓程序運行這很奇怪...新程序在https://gist.github.com/D-codex/009a5afa391c760b7ecc88f8109662c0,它將輸出爲輸入二進制樹中的節點數:3 節點#0的詳細信息輸入數據:節點1的詳細信息 輸入數據:完全跳過第一個節點的數據輸入... –

+0

這並不令人意外。對不起,但你的代碼是一大堆怪異的代碼。但這不是我們爲您調試亂碼的網站。也許我稍後會看看......但這可能意味着星期二... – GhostCat

+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],然後可以訪問數據。

+0

bTree。binaryTree []已經在Tree類中初始化了,節點的數據在節點的構造器中被初始化爲「」。 –

+0

你是否嘗試過你的代碼@Kiran Kumar它返回'線程中的異常「main」java.lang.ArrayIndexOutOfBoundsException:0' –

+0

是的我知道,但我指導他爲什麼NullPointerException發生,就是這樣,我並不擔心關於他的邏輯。 –

相關問題