简体   繁体   中英

ADT function keeps returning null and i have NO clue why

my insert function keeps returning null and printing out "not found" for some reason. I feel like it has something to do with the fact that head is null and is not assigned to be a new Node.. but i can't do this in findKey because it is a private class:

// findKey()
// returns a reference to the Node at key in the LinkedList; otherwise returns null
private Node findKey(String key){
    Node N = head;
 while(N != null){
    N = N.next;
    if(N.key.equals(key) && N.key != null){
      return N;
  }
    }
    return null;

}
public String lookup(String key){
 if(findKey(key) == null){
     System.out.println("not found");
     return null;
  }else{
  Node N = findKey(key);
  return N.value;
  }
}

public void insert(String key, String value)
  throws KeyCollisionException{

  if(lookup(key)!= null ){
     throw new KeyCollisionException(
        "cannot create duplicate key");
  }
  if(head == null){
    head = new Node(key,value);
    return;
    }else{
    Node iter = head;
    while(iter.next != null){
    iter = iter.next;
    }
    Node N = new Node(key,value);
    iter.next = N;
    numItems++;
    }

}

You're moving to the next node too early in findKey which means you will never check the head node to see if it contains the required key and that you will get NullPointerExceptions if the key isn't found.

Just move the N = N.next; line down a bit. Also, swap the order of checks around in the if statement (although your Node constructor should prevent null keys anyway):

private Node findKey(String key){
    Node N = head;

    while(N != null){
        if(N.key != null && N.key.equals(key)){
            return N;
        }
        N = N.next;
    }
    return null;
}

You should use the this keyword in your constructor:

Node(String key, String value){
    while(key != null && value !=null){
    this.key = key;
    this.value = value;
    next = null;
}

And "while" is not necessary. Using "if" is better.

There are a couple of problems which could be leading to your issues. First off, because you use the same variable names for your class's fields and the constructor's parameters, you should be using the this keyword to distinguish between the 2. Also, you should change the while loop to an if statement because you are not looping through anything. If the if statement should fail, you should set some default values or throw a NullPointerException . Change:

String key;
String value;

Node(String key, String value) {
    while(key != null && value != null) {
        key = key;
        value = value;
    }
}

to

String key;
String value;

Node(String key, String value) {
    if(key != null && value != null) {
        this.key = key;
        this.value = value;
    }
    else {
        throw new NullPointerException("Initial values cannot be null");
    }
}

Secondly, you have

if(lookup(key) != null ) {
    throw new KeyCollisionException("cannot create duplicate key");
}
if(head == null) {
    head = new Node(key,value);
    ...
}

It is good that you set head to a new node if it is null, but you are doing it after making a method call which will result in head being referenced inside of findKey() . You should switch the order of these operations to prevent your NullPointerException .

if(head == null) {
    head = new Node(key,value);
    ...
}
if(lookup(key) != null ) {
    throw new KeyCollisionException("cannot create duplicate key");
}

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