TheNodeCommode
TheNodeCommode

Reputation: 93

Nullpointer runtime exception

Cant seem to find the problem, a bit lost in my code right now... could really use a better eye. Really appreciate it.

Here is the error:

Exception in thread "main" java.lang.NullPointerException
    at Heap.<init>(Heap.java:16)
    at Heap$1.<init>(Heap.java:131)
    at Heap.main(Heap.java:131)

http://codepaste.net/jogkx2

public abstract class Heap {

    private List heap_array; // Arraylist containing heap.
     private List<ELEM> priority_queue; //ArrayList containing ELEMS.
     private int heapsize; // Maximum heapsize of the heap.
     private int n; // Number of elements currently in the heap.
     private int last_elem = heap_array.size()-1; // last elem in the heap


     public Heap(int s, int n) {
        heap_array = new ArrayList();
          priority_queue = new ArrayList<ELEM>();
          s = heapsize;
          n = this.n;
    }

    protected int returnMaxPriority(){
        int max = priority_queue.get(priority_queue.size()-1).getLocation();
        return max;
        }

    public void shiftDown(int i) {
        int leftChild = leftChild(i);
        int rightChild = rightChild(i);
        int largest = i;

            // Max Heapify
        if (leftChild < heap_array.size() 
          && !aboveOrEqual(largest, leftChild)) {
            largest = leftChild;
        }
        if (rightChild < heap_array.size() 
          && !aboveOrEqual(largest, rightChild)) {
            largest = rightChild;
        }

        if (largest != i) {
            switchElems(largest, i);
            shiftDown(largest);
        }
    }

    public void insert(Object obj, int priority) {
              heap_array.add(obj);
              int object_i = 0;
             while (object_i > 0 && !aboveOrEqual(parent(object_i), object_i)) {
          switchElems(parent(object_i), object_i);
          object_i = parent(object_i);
             }
            // enqueue(object_i, priority);
    }

    public Object removeLast() {
        if (heap_array.size() > 0) {
            switchElems(0, last_elem);
            Object result = heap_array.remove(last_elem);
               shiftDown(0);
            return result;
        } else {
            return null;
        }
    }


    public Object get(int index) {
        return heap_array.get(index); //Return object
    }

    public int heapsize() {
        return heap_array.size();
    }

    protected int parent(int i) {
        return (i - 1) / 2;
    }
     protected void update_N(int n){
             n = this.n;
     }

      protected void update_Size(int size){
             heapsize = size;
     }

    protected int leftChild(int i) {
        return 2 * i + 1;
    }

    protected int rightChild(int i) {
        return 2 * i + 2;
    }

    protected void switchElems(int i, int j) {
        Object tmp = heap_array.get(i);
        heap_array.set(i, heap_array.get(j));
        heap_array.set(j, tmp);
    }


    public void enqueue(int object_i, int priority) {
           ELEM tmp = new ELEM(priority, object_i);
          priority_queue.add(object_i, tmp);
        }


     public int dequeue() {
     if (heap_array.size() > 0) {
     int max_location = returnMaxPriority();
         heap_array.remove(max_location);
        return max_location;
        }
        else{return -1;}
     }

     public void changeWeight(int object_i, int priority){
           ELEM tmp = new ELEM(priority, object_i); 
           priority_queue.set(object_i, tmp);
     }

     protected abstract boolean aboveOrEqual(int value1, int value2);

    public static void main(String[] args){
        Heap h = new Heap(100, 20) {
            protected boolean aboveOrEqual(int value1, int value2) {
                return ((Integer)get(value1)).intValue() 
                     >= ((Integer)get(value2)).intValue(); //Compares the objects int values.
            }
        };

        System.out.println("Enter a list of numbers, then type quit: ");


          for (int i = 0; i < 100; i++) {
            h.insert(new Integer((int)(100 * Math.random())), i);
        }
    }
}

class sortPriorityArray implements Comparator<ELEM> {
    @Override
    public int compare(ELEM s1, ELEM s2){
        int value1 = s1.getPriority();
        int value2 = s2.getPriority();

        if(value1 < value2){
        return 1;
        }
        else if(value2 > value1){
        return -1;    
        }
        return 0;
    }

If you could also take a look at my insert function and returnmax. The class ELEM contains priority and location so I can dequeue in the proper order by sorting the priority_queue by priority then dequeue the highest priority first.

Upvotes: 0

Views: 89

Answers (3)

Peter Lawrey
Peter Lawrey

Reputation: 533530

The exception is being thrown here

private int last_elem = heap_array.size()-1; // last elem in the heap

At this point heap_array has not been initialised and so it is null. The simplest solution is to move this to the constructor or set it to

private int last_elem = -1; // last elem in the heap

as this will do the same thing.

BTW If you are wondering how you could have worked this out for yourself, you could have stepped through the code in your debugger, it is the button next to Run in your IDE.

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691755

private int last_elem = heap_array.size()-1; // last elem in the heap

This line calls size() on a list that hasn't been initialized yet.

Upvotes: 1

Shrike
Shrike

Reputation: 155

n = this.n; is probably the culprit. You are trying to refer to this.n before initializing it. You'll want to avoid using this in constructors unless you are initializing (not referencing) members of this.

EDIT: I guess you meant this.n = n;? Just saw your comment, I guess that explains it. :)

Upvotes: 1

Related Questions