12 Replies - 566 Views - Last Post: 08 January 2012 - 04:30 PM Rate Topic: -----

#1 metalover  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-January 12

NullPointerException error

Posted 08 January 2012 - 07:10 AM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: NullPointerException error

#2 metalover  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-January 12

Re: NullPointerException error

Posted 08 January 2012 - 07:20 AM

just to add that line 83 here is line 81 in the post
Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: NullPointerException error

Posted 08 January 2012 - 07:20 AM

Do you ever instantiate a Node object? That would look something like:

root = new Node( name, size );
Was This Post Helpful? 0
  • +
  • -

#4 metalover  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-January 12

Re: NullPointerException error

Posted 08 January 2012 - 07:26 AM

Yes in add method
Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: NullPointerException error

Posted 08 January 2012 - 07:49 AM

I missed that.

Well, the obvious answer is that in your largest( Node size ) method, size.left can be null. You might add in some test code to verify that is what's happening as well as determining whether size.right could also be null.

Once you've determined that's the problem, figure out why size.left (and size.right?) is/are null when you weren't expecting them to be and either trap or correct those conditions so that your code will run.
Was This Post Helpful? 0
  • +
  • -

#6 metalover  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-January 12

Re: NullPointerException error

Posted 08 January 2012 - 08:30 AM

I tryed to do this but it still giving the same error.

private double largest(Node size)
    {
        double leftSide = largest(size.left);
        double rightSide = largest(size.right);
        try
        {
            
            if (size == null)
            {
                return 0;
            }
            else if (size.size > leftSide && size.size > rightSide)
            {
                return size.size;
            }        
            else if (leftSide < rightSide)
            {
                return rightSide;
            }

        }
        catch(Exception e)
        {
            System.out.println("Error");
        }
        return leftSide;
    }

Was This Post Helpful? 0
  • +
  • -

#7 metalover  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-January 12

Re: NullPointerException error

Posted 08 January 2012 - 08:40 AM

also tryed something like
if (leftSide == null)
        {
            return 0;
        }
        else if(rightSide == null)
        {
            return 0;
        } 


but it is not helping as well.
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7807
  • View blog
  • Posts: 13,200
  • Joined: 19-March 11

Re: NullPointerException error

Posted 08 January 2012 - 08:57 AM

View Postmetalover, on 08 January 2012 - 10:30 AM, said:

I tryed to do this but it still giving the same error.

If this line is throwing the exception:
        double leftSide = largest(size.left);


then error chaecking after that line isn't going to help.

size is null coming to this method.
Was This Post Helpful? 0
  • +
  • -

#9 metalover  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-January 12

Re: NullPointerException error

Posted 08 January 2012 - 09:02 AM

Yeah i notice that, im kinda in dead end right now...
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7807
  • View blog
  • Posts: 13,200
  • Joined: 19-March 11

Re: NullPointerException error

Posted 08 January 2012 - 09:35 AM

So if you're calling this method with a null argument, you need to look at the calls to this method and trace back to see how that happens. Don't forget the recursive calls. (is it possible for size.left to be a null node?)
Was This Post Helpful? 0
  • +
  • -

#11 metalover  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-January 12

Re: NullPointerException error

Posted 08 January 2012 - 02:45 PM

Could you give me some more tips ? Still cant find a solution...
Was This Post Helpful? 0
  • +
  • -

#12 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7807
  • View blog
  • Posts: 13,200
  • Joined: 19-March 11

Re: NullPointerException error

Posted 08 January 2012 - 02:51 PM

Try looking at this a little closer. What do you think this means?


Quote

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)

Was This Post Helpful? 0
  • +
  • -

#13 metalover  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-January 12

Re: NullPointerException error

Posted 08 January 2012 - 04:30 PM

problem solved i rewrite all the method from the begin and it worked correctly thanks for your time!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1