public class BST
{
private Node root;
public void add(String name, double size)
{
Node temp = new Node(name, size);
if (root == null)
{
root = temp;
}
else
{
Node before = root;
Node after = root;
while (after != null)
{
before = after;
int cmpafter = name.compareTo(after.value);
if (cmpafter < 0)
{
after = after.left;
}
else
{
after = after.right;
}
}
int cmpbefore = name.compareTo(before.value);
if (cmpbefore > 0)
{
before.right = temp;
}
else
{
before.left = temp;
}
}
}
private void inOrder(Node size)
{
if (size == null)
{
return;
}
else if (size != null)
{
inOrder(size.left);
System.out.println(size.value + " " + size.size);
inOrder(size.right);
}
}
public void inOrder()
{
inOrder(root);
}
private int count(Node count)
{
if (count == null)
{
return 0;
}
else
{
return (count(count.left) + count(count.right) +1);
}
}
public int count()
{
int count = count(root);
return count;
}
private double largest(Node size)
{
double leftSide = largest(size.left);
double rightSide = largest(size.right);
if (size == null)
{
return 0;
}
else if (size.size > leftSide && size.size > rightSide)
{
return size.size;
}
else if (leftSide < rightSide)
{
return rightSide;
}
else
{
return leftSide;
}
}
public double largest()
{
double largest = largest(root);
return largest;
}
class Node
{
String value;
double size;
Node left;
Node right;
Node(String name, double sizeIn)
{
value = name;
size = sizeIn;
left = null;
right = null;
}
}
}
above is the BST class
import java.io.*;
import java.util.*;
public class Ascii
{
//Sequential text file input of characters
static BufferedReader fileInput;
public static void main(String args[]) throws IOException
{
BST newTree = new BST();
String inputString;
String name;
double size;
int index = 0;
StringTokenizer tokens;
//Array to store 32 Elements
Elements [] ElementsArray = new Elements[32];
try
{
fileInput = new BufferedReader(new FileReader("ArrayList.txt"));
inputString = fileInput.readLine();
//Read the file until is null
while(inputString != null)
{
//Spliting the string using the String Tokenizer
tokens = new StringTokenizer(inputString);
//Extracting the first field
name = tokens.nextToken();
//Exctracting the second field
size = Double.parseDouble(tokens.nextToken());
ElementsArray[index] = new Elements(name, size);
index++;
inputString = fileInput.readLine();
}
}
catch(java.lang.Throwable t)
{
System.out.println("NullPointerException Error");
}
//Closing the file
fileInput.close();
System.out.println("File read and closed");
for (int i = 0; i < ElementsArray.length; i++)
newTree.add(ElementsArray[i].getName() , ElementsArray[i].getSize());
newTree.inOrder();
System.out.println("count " + newTree.count());
System.out.println("Largest" + newTree.largest());
}
}
and above is the ascii class
the problem is that the program compiling normaly but when i try to run i receive this error message on the screen
java.lang.NullPointerException
at BST.largest(BST.java:83)
at BST.largest(BST.java:83)
at BST.largest(BST.java:83)
at BST.largest(BST.java:83)
at BST.largest(BST.java:83)
at BST.largest(BST.java:105)
at Ascii.main(Ascii.java:56)
and the at the BST its marking the
double leftSide = largest(size.left);and say that java.lang.NullPointerException: null
the method in the BST class need to search the binary search tree to find and return the largest radius, the method has to return a double.

New Topic/Question
Reply



MultiQuote




|