简体   繁体   中英

Null Pointer Exception in Recursive method

I am trying to create a method ' headSet ' that creates and returns a new TreeSet , set , of values which are all values in the called TreeSet which are less than the parameter element 'before'.

I can get all the correct traversals and I debugged, in Net Beans, the new set does contain all the values that it should before the exception is thrown. I just can't figure out why when i call headSet(n.right,before,set) .. specifically n.right .. it breaks. It would work fine if it didn't break.

Edit: When I run the program with problem line , headSet(n.right,before,set) , then all 3 headSet() method calls in the main recursive helper are in the stack trace. When I comment out that line, there are no problems other than an incorrect tree traversal.

This is the main public called method that triggers the recursive helper:

public SortedSet<E> headSet(E before){
  SortedSet<E> set = new SearchTreeSet<E>();
  headSet(root, before, set);
  return set;
}

where root is the first node in the called TreeSet .

The main recursive helper:

private void headSet(Node n, E before, SortedSet<E> set) {
  int comp = myCompare(n.data, before);

  if (comp < 0){ //n.data is less than before
    // add node n to the new set
    if (n.data != null) { //It shouldn't be null but I just wanted to eliminate NPE sources
        set.add(n.data);
    }
    // all nodes to the left are added automatically with a separate recursive function
    headSet(n.left, set);

    // test nodes to the right

    //////////////The next statement forces a null pointer exception ////////
    headSet(n.right, before, set);
  }
  // n.data is greater than or equal to 'before'
  else {

        // move to the left and retest
        headSet(n.left, before, set);
  }
}

The second recursive function doesn't compare, it just adds all node branches to the new Sorted Tree Set 'set'

private void headSet(Node n, SortedSet<E> set){
  if (n.data != null){ // 'if statement' is to eliminate NPE sources, it normally shouldn't be null
    set.add(n.data);
  }
  if (n.left != null) { headSet(n.left, set);  }
  if (n.right != null) { headSet(n.right, set); }
}

RESOLVED : Thanks guys! That did it.. I can't believe I didn't see it.

Here's what I changed to fix the problem:

if (n.left != null) {
   headSet(n.left, set);
}

if (n.right != null) {
   headSet(n.right, before, set);
}

And also

if (n.right != null) {
   headSet(n.right, before, set);
}

First of all, I don't think you are going to achieve what you have planned for with SortedSet. Because when you add object to SortedSet it sorts the internal order of objects according to compareTo method defined by the object you are adding to it. Now in your case simplest thing to do is to implement Comparable to n.data class. When you do that you can use logic what you had defined in myCompare method. Now add n.data to SortedSet in any order and SortedSet will organise them using its natural order. If you really want to maintain order programmatically then use List. Now lets say you got rid of NPE and then you want to print n.data stored in Set and want to verify that your sorting algorithm had worked or not, which you can not do because set will return the list of object according to its natural sorting order.

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