I'm trying to initialize a Comparable array using an int to determine initialize the size of the array. However, after I initialize the array, the array is null. If I try and add things to the array, I get a NPE.
Below is some relevant code (I realize it is not type-safe, that's not a concern atm)
public class Heap implements MyHeap{
int capacity;
int size;
Comparable[] heap;
public void Heap(){
size = 0;
capacity = 10;
heap = new Comparable[capacity];
}
public void printHeap(){
for (int i = 1; i <= size; i++){
System.out.println(heap[i]);
}
}
public void insert (Comparable object){
size++;
heap[size] = object;
bubbleUp(size);
}
EDIT: I've changed my code to reflect the suggestion of the first answer. I set the value for the int capacity before I initialize my array using that int, and the array is still null and has null length. I even initialized the array using an int as the parameter as opposed to the variable capacity and I got the same behavior.