public class InnerClass implements UsingInterface { public class tree //inner class { int number; tree left; tree right; } private tree root; public InnerClass() { System.out.println("Inner Class constructer called and root is null"); root = null; } private void insert(int number,tree leaf) //helper method for binary tree insert { if(number < leaf.number) { if(leaf.number != null) { insert(number,leaf.left); } else { leaf.left = new tree(); leaf.left.number = number; leaf.left.left = null; leaf.right.right=null; } } else if(number > leaf.number) { if(leaf.number != null) { insert(number,leaf.right); } else { leaf.right = new tree(); leaf.right.number = number; leaf.right.left = null; //creates new left child and sets it to null leaf.right.right=null; //creates a new right child and sets it to null } } } public void insert(int key) { if(root!=null) { insert(key,root); } else { root = new tree(); root.number = key; root.left = null; root.right = null; } } public void search(int key) { search(key,root); } private void search(int key,tree node) { if(node==null) { System.out.printf("%s ", "Tree is empty"); } else if(key == node.number) { System.out.printf("%s,%i ", "The number was node.number found"); } else if(node != null) { if(key < node.number) { search(key,node.left); } else if(key > node.number) { search(key,node.right); } else System.out.println("The number was not found"); } } } Inner Class constructer called and root is null Exception in thread "main" java.lang.NullPointerException at InnerClass.insert(InnerClass.java:23) at InnerClass.insert(InnerClass.java:42) at InnerClass.insert(InnerClass.java:59) at Inheritance.pushNumber(Inheritance.java:10) at MainClass.main(MainClass.java:9)
I am playing around with binary trees in Java. I am used to using pointers in C++ and wanted to see how they work in Java. However I keep getting errors, do i need to create the inner class somewhere? If so why?