1

I'm trying to put together a quad tree for a homework assignment, similar in function to a binary tree, and I got down to implementing the get() method, where it returned a key's value if the value existed, otherwise it returned null. However, with my given test cases, I keep receiving a nullPointerException, and I can't seem to figure out why. Any and all help is appreciated.

Here's my get() method:

public V get(Coord<X,Y> key) throws IllegalArgumentException {
    checkKey(key);
    Position<Entry<Coord<X, Y>, V>> p = treeSearch(tree.root(), key);
    if(tree.isExternal(p)) { return null; }
    return p.getElement().getValue();
  }

Now, my homework file says that this should be almost identical to this get method(also in the homework file):

public V get(K key) throws IllegalArgumentException {
    checkKey(key);                          // may throw IllegalArgumentException
    Position<Entry<K,V>> p = treeSearch(root(), key);
    rebalanceAccess(p);                     // hook for balanced tree subclasses
    if (isExternal(p)) return null;         // unsuccessful search
    return p.getElement().getValue();       // match found
  }

with some exceptions for the types.

In the test cases, it makes a temporary tree with random integers as an entry's value, and these:

assertEquals(null, m.get(new Coord<>(0,2)));
assertEquals(null, m.get(new Coord<>(-6,-5)));
assertEquals((int)3, (int)m.get(new Coord<>(-5,-6)));
assertEquals((int)6, (int)m.get(new Coord<>(7,7)));
assertEquals((int)0, (int)m.get(new Coord<>(0,0)));

would always return a nullPointerException for my get() method, whereas if used on the example, it would return the key's value.

0 Answers0