Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Problem with Binary Search Tree

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with Binary Search Tree

    Hi, I'm new to the forum and am not sure if this is the right place to post this, but I'm getting a problem with the following Binary Search Tree:

    import java.awt.*;
    import java.awt.event.*;
     
    import javax.swing.*;
     
    import java.util.Comparator;
    class BST{
    private BSTNode t=null;
    private BSTNode l = null;
    private BSTNode r = null;
    private BSTNode node;
    private StringComparator comp;// how to compare nodes
    public BST(Comparator c){
     
     
    }
     
    // constructor
    public BSTNode insert(Object newData){
    BSTNode temp=t;
    if (t == null)
    {
        t=new BSTNode(newData);
        t.left= new BSTNode(null);
        t.right= new BSTNode(null);
        return t;
    }
    else
    {
     
        while (temp.data != null)
        {
            if (comp.compare(temp.data, newData)<0)
                {
                    System.out.println("going left");
                    temp=temp.left;
                }
                else
                {
                    System.out.println("going right");
                    temp=temp.right;
                }
     
        }
        temp.data=newData;
        temp.left= new BSTNode(null);
        temp.right= new BSTNode(null);
        return temp;
    }
    //  if (comp.compare(t.data, newData)<0)
    //  return t.left = insert(newData);
    //else return t.right = insert(newData);
    }
     
     
    // can check if comp is null before insert
    public void setComparator(StringComparator c){
    if (t == null)
        c.compare(t.data, node.data);
    }
    // only allow if tree empty
    public void clear(){
     
        t = null;
    }// empty tree
    public BSTNode find(String key)
    {
     if (t == null)
     return null;
    if (key == t.data)
     return t;
    if (comp.compare(key, t.data)<0)
    return find(key);
    else
        return find(key);
    }
    public void getText(JTextField t, String key)
    {
    t.setText(key);
    }
    public String toString()
    {
    String fail= "The object was not found.";  
    return fail;}
    }

    In particular I'm getting a null pointer exception on line 33 (if (comp.compare(temp.data, newData)<0)).

    Any idea why this is happening? Help would be much appreciated!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problem with Binary Search Tree

    Use the System.println method and add some println's in there to monitor the command line and see what object on that line could be null. NullPointerException indicates a reference is null...so when you find anything that is null, backtrack and make sure it is assigned properly (either to a valid reference or creating a new one). For future reference, posting an SSCCE with descent indenting helps
    Last edited by copeg; April 25th, 2012 at 09:02 PM.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problem with Binary Search Tree

    And please be forthright when crossposting - many (including myself) do not appreciate writing responses to posts which may have been answered or discussed elsewhere without their knowledge

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/58889-problem-binary-search-tree.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


Similar Threads

  1. Building a binary search tree
    By Herah in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 28th, 2011, 07:29 AM
  2. Balanced Binary Search Tree
    By D3158 in forum Object Oriented Programming
    Replies: 1
    Last Post: June 24th, 2011, 09:14 AM
  3. Binary Search Tree
    By lex25288 in forum Algorithms & Recursion
    Replies: 3
    Last Post: January 19th, 2011, 09:10 AM
  4. Data Structures(Binary Search Tree to AVL Tree)ASAP
    By jfAdik in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 5th, 2010, 03:58 AM
  5. Binary Search Tree
    By Koren3 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 12th, 2009, 09:27 AM