0

Whatever I try to do with the line bTree.binaryTree[i].Data=in.nextLine(); in framework class results in

Enter the Number of Nodes in the Binary Tree :5 Exception in thread "main" java.lang.NullPointerException Details for node#0 at binaryTree.framework.intialize(framework.java:19) Enter Data : at binaryTree.main.main(main.java:9) Process finished with exit code 1

Github gist : https://gist.github.com/D-codex/0051d59abb91fafc73fbec0fa7dad356

EDITED : https://gist.github.com/D-codex/009a5afa391c760b7ecc88f8109662c0 (After following GhostCat's suggestion)

I have done my best to initialize almost everything to avoid having 'null' anywhere but this exception keeps popping up

Node Class

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)+")";
}
}

Tree class

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;
        }
    }
}
}

Framework class

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);
}
}

Main class

package binaryTree;

public class main {
public static void main(String Args[]){
    framework Framework=new framework();
    Tree binaryTree=Framework.intialize();
    Framework.displayAll(binaryTree);
}
}
Dheshan M
  • 20
  • 5
  • 5
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – YCF_L Dec 23 '16 at 14:45
  • Follow the java naming conventions, if you don't know, go and read it. – Kiran Kumar Dec 23 '16 at 14:50
  • Thanks for the quick accept. When I find some time next week I will put up some suggestions for you. – GhostCat Dec 23 '16 at 17:32

2 Answers2

1

The error was at the place where you are accessing the tree object bTree.binaryTree[i].data, You are trying to access the property of 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);

Here in this case you created an array of the objects, but the array is not filled with any objects, access such locations will end up in NullPointerException. Create an object using new operator and then assign to the location binaryTree[i] and then you can access the data.

Kiran Kumar
  • 936
  • 5
  • 20
  • bTree.binaryTree[] is initialized in the Tree class already,The Data for the node is initialized to " " in node's contructor. – Dheshan M Dec 23 '16 at 15:15
  • did you try your code @Kiran Kumar it return `Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0` – YCF_L Dec 23 '16 at 15:15
  • Yeah I know, But I am guiding him about why the NullPointerException was occurring, that's it, I am not worried about his logic. – Kiran Kumar Dec 23 '16 at 15:17
  • this not provide the solution @Kiran Kumar, this is just explanation, he or she can learn about the NullPointException in the link :) – YCF_L Dec 23 '16 at 15:24
  • You are right @MrLY But , He did not told us what he want to achieve or what he wanted to do right? – Kiran Kumar Dec 23 '16 at 15:26
  • you said in the comment `//uncomment this line its will work :) so what you consider this a solution or what?` – YCF_L Dec 23 '16 at 15:33
1

Your problem is here:

binaryTree=new node[count];

This only creates a new but empty array.

You first have to iterate that array and insert a Node object in each slot!

Besides: you want to read about java coding style guides. Class names always start Uppercase for example.

GhostCat
  • 127,190
  • 21
  • 146
  • 218
  • tried your suggestion now i can get the program to run It's weird... the new program is at https://gist.github.com/D-codex/009a5afa391c760b7ecc88f8109662c0 and it results in the output as Enter the Number of Nodes in the Binary Tree :3 Details for node#0 Enter Data :Details for node#1 Enter Data : Entirely Skipping the first node's data entry... – Dheshan M Dec 23 '16 at 15:30
  • This is not a surprise. Sorry but your code is one huge mess of weird code. But this is not a site where we debug your messy code for you. Maybe I will have a look later... But that might mean Tuesday... – GhostCat Dec 23 '16 at 15:58
  • Thanks for your effort.I found the error and it's good and running now;). – Dheshan M Dec 23 '16 at 16:46