1

Hoping you guys can figure out why im getting a null pointer exception with what I can provide, the program has several classes and method but this is the one that is breaking.

public void search(Node node, String sData, int iData)
{
    if (sData.equals(node.getString()) && (iData == node.getInt()))
    {
        System.out.println("Nailed it");
    }else if (sData.compareTo(node.stringData) < 0)
    {
        search(node.left, sData, iData);
    }else if (sData.compareTo(node.stringData) > 0)
    {
        search(node.right, sData, iData);
    }

}

the Node that is getting input at first is the root and then it goes left or right from there through recursion, but the line that says its erroring is the if statement up top. Cannot figure out what is wrong sData is just a standard string input when the method is called and iData is just an int thats input as well. Cannot figure it out =/ thanks for any help

2
  • node may be null if a node does not have a right node, ie. the node is a leaf. So you have to check for that null. Commented Jan 31, 2012 at 23:47
  • 2
    My guess would be that the value you are looking for isn't in the search tree (or your initial if statement isn't working as expected), eventually causing node.left or node.right to become null when it reaches a leaf node. The next call of search will try and call node.getString() which would then throw the null pointer exception. Commented Jan 31, 2012 at 23:48

2 Answers 2

4

You need to add the following at the top of your method:

if (node == null) return;

This would ensure that the method returns gracefully if you search for something that doesn't exist in the structure. Otherwise with your existing code, you'll encounter NullPointerExceptions when the method encounters leaf nodes.

Sign up to request clarification or add additional context in comments.

3 Comments

You rock...so hard. my friend
Not sure silently returning is what's best here; that caused his troubles in the first place. Catching the exception would be on option, printing a debug-statement before returning another. But since his code doesn't check for nodes that aren't in the tree, maybe silently returning is the desired behavior.
Since the method has a return type void, I think returning silently is the desired behavior in this case. Ideally, the method should return the node, if found, or null.
4

Two possibilities: either sData or node are null; your recursive calls potentially pass null nodes, that's what I'd start with. I can't really tell more from your code, sorry.

Additionally, consider using a debugger to step through your code if you can't find the error. That usually helps with code like this.

1 Comment

Thank you! I didn't even think that it was erroring on the recursive part I assumed it was breaking before that on the initial call! thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.