简体   繁体   中英

Java Hash Table giving NullPointerException when checking if string is not null

I have a hash table that is implemented using a node array, where the node contains a string and a boolean to check if there was a string there before for when searching for an element after elements have been deleted. I am getting a nullpointerexception at a place that I don't understand how I possibly could, in this line:

while(T.getElement(place) != null)

This is my code to do the loop that is throwing the exception.

Get Element is here:

public String getElement(int index){
        if (index != lastProbed)
            probeCount++;
        lastProbed = index;
        return table[index].str;
}

And this is my node class:

class Node {

boolean hadStr;
String str;


public Node() {
    str = null;
    hadStr = false;
}

}

I don't understand how I could be getting a null pointer in a place where I am just checking if a string is null.

Why not

public String getElement(int index){
        if (index != lastProbed)
            probeCount++;
        lastProbed = index;
        if (table[index] != null) {
           return table[index].str;
        }
        return null;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM