开发者

Java: checking if Comparable is not null returns NullPointerException

I have a big problem with this code and I have no idea how to cause it:

while(tree.find(indexreg)!=null){
        //do stuff
    }

For some reason, comparing tree.find(indexreg) with null causes a NullPointerException. Since this is a college project I have to use my own binary tree implementation instead of the one provided by Java. tree is a BinarySearchTree and indexreg is a Comparable object, that was already initialized. This is the code for find in the BinarySearchTree class:

public Comparable find(Comparable x) {
        return elementAt(find(x, root));
    }

It looks for the object in the tree and it returns null if it doesn't find it (I don't think you can return an empty Comparable object). I tried Googling but I didn't find an useful answer. Does anyone know how to make开发者_如何学JAVA this code work?


I don't think the problem has anything to do with your Comparable.

If the line while(tree.find(indexreg) != null) { throws a NullPointerException, it must be because tree is null. No other possibilities are credible.

  • Comparison of an object reference with null using == or != will NOT throw an NPE. So even if tree.find(...) returned a null, that can't be the cause of this exception.

  • Passing a null value as a method argument will NOT throw an NPE. So if indexreg was null, that wouldn't cause this exception. (An NPE could be thrown by the find method or something it calls, but the stacktrace will not show a different line in a different method as the origin of the exception.)


(I could be misinterpreting the question. I'm assuming that the OP means "throws" when he says that the line "causes" the exception.

Unfortunately, the OP is only posting snippets of code and hasn't shown us the stacktrace ... which is the critical piece of evidence.)


public Comparable find(Comparable x) {
    return x == null ? null : elementAt(find(x, root));
}

fyi this is equivalent to:

public Comparable find(Comparable x) {
    if (x == null) return null;
    return elementAt(find(x, root));
}

You nay also consider improving the clarity of your code: You have a method call and a test combined. While this is not "bad" per se, IMHO it would be cleaner to separate the two, AND get a hold of the value returned in case you want to do something with it, like this:

for (Comparable<?> result = tree.find(indexreg); result != null; result = tree.find(indexreg)) {
    //do stuff with variable "result"
}

It just makes it more obvious what is controlling the loop.

There is another way to get the result, but it's considered "bad coding style" by some; that is to assign and test in one:

Comparable<?> result;
while ((result = tree.find(indexreg)) != null) {
    //do stuff with variable "result"
}

Some believe that you should avoid this style of coding. I tend to agree with them.


Possibly indexreg is null, not initialized at all. You should certainly code find() more defensively as suggested by @Bohemian but this may be the underlying problem. Or see next comment below.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜