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 5 of 5

Thread: NullPointerException when comparing string in binary tree

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default NullPointerException when comparing string in binary tree

    HI there, please help out with a bug i encountered during my binary tree assignment.

    i searched the net for a binary tree code and thanks to CHINMAY LOKESH, i found one with a tree displays in it. i altered the code a bit to compare "string" rather than "integer". tree displaying, Inserting, deleting, and finding was doing fine when i encounter a bug when finding a value that is not inside the tree. the description are as follows...

    this is the number i entered into the tree.

    das
    dd
    5g
    3213
    ds6
    asd
    o56o
    hj23
    0ghj
    asd
    1

    the Binary Tree was Created Successfully! i check the displays of the tree.

    when i find the number "2"
    it was doing what it supposed to by saying cannot find the number "2"

    but when i try to find the number "22"
    nothing occurs and this error message was displayed...

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Tree.find(Tree.java:14)

    this is the code in the applet...
    public Node find(String val)
        {
                Node current = root;
    line 14:  while(val.equals(current.item) == false)
                {
                if(val.length()>current.item.length())
                {
                	current = current.rightChild;
                }
                else if(val.length()<current.item.length())
                {
                	current = current.leftChild;
                }
                else
                {
                	if(val.compareToIgnoreCase(current.item)<0)
                		current = current.leftChild;
                	else
                		current = current.rightChild;
                	if(current == null)
                    return null;
                }
            }
            return current;
        }

    the odd thing is.... the value i use for finding in the range of 10 till 59 gave the same errors, but finding for 9 or 60 displays cannot find which is correct by the way...

    another thing i noticed which is also weird...

    if i did the find method for 10 till 60 after i inserted a number of "30"
    the programs work fine and gave a message that those numbers cannot be found...

    ??? i have no ideas what's happened... any ideas from the community here?

    this my first time posting a thread here, if you need extra info do pm here.
    thanks in advance and have a nice day.

    ps. tetkun.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: NullPointerException when comparing string in binary tree

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Tree.find(Tree.java:14)
    There is a variable with a null value on line 14. Look at line 14 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 14 and print out the values of all the variables on that line.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    tetkun (March 19th, 2013)

  4. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException when comparing string in binary tree

    Hi Norm,

    Thanks for the reply

    i've check the value using system.out.println
    the value for "val" is "aa",
    the value for "current.item" is "X622"

    it's clear that both of them are not null... but when i run them through the code

    public Node find(String val)
        {
                Node current = root;
    line 14:  while(val.equals(current.item) == false)
                {
                if(val.length()>current.item.length())
                {
                	current = current.rightChild;
                }
                else if(val.length()<current.item.length())
                {
                	current = current.leftChild;
                }
                else
                {
                	if(val.compareToIgnoreCase(current.item)<0)
                		current = current.leftChild;
                	else
                		current = current.rightChild;
                	if(current == null)
                    return null;
                }
            }
            return current;
        }

    the same errors came out...
    pls be noted this error doesn't come out if i enter "1234" as the value for "val".
    i have no idea what happened... any ideas???
    Last edited by Norm; March 19th, 2013 at 06:23 AM. Reason: fixed code tag

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: NullPointerException when comparing string in binary tree

    the same errors came out...
    What variable has the null value?
    Why doesn't that variable have a valid value?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException when comparing string in binary tree

    Thanks for your suggestion norm... i figure it out by doing a bit searching on the error message on the net. it seems that though the first value (val & current.item) was not null, which in case made me puzzled why the heck presented the null errors, the last value (current.item) when it search down the tree would eventually came to a null while looping down the tree. i fix it by changing the while statement with (while current != null) and use an if statement for the condition (if (val.equalsIgnoreCase.(current.item))) inside the while loop. then use "break" to end the looping when the value was found. thanks for pointed me out to look for the null value that occurs... pulled out few hairs while thinking about it but i'm glad that my tree is finally bug free... thank you and have a nice day.

Similar Threads

  1. Binary Search Tree inorder tree traversal
    By Maukkv in forum What's Wrong With My Code?
    Replies: 17
    Last Post: January 26th, 2013, 05:28 PM
  2. Help with this binary tree
    By sim18 in forum What's Wrong With My Code?
    Replies: 23
    Last Post: November 29th, 2012, 03:10 AM
  3. Binary Tree Search[HELP]
    By husain2213 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 13th, 2012, 11:33 PM
  4. need help with binary tree
    By vash0047 in forum Java Theory & Questions
    Replies: 5
    Last Post: July 12th, 2010, 08:23 AM
  5. 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

Tags for this Thread