I'm trying to insert values into an initially empty binary heap.
This is the relevant code:
public class minHeap
{
int[] array;
int n;
public void minHeap()
{
array = new int [16];
n = 0;
}
public void insert(int x) {
//if (n == array.length - 1)
//enlargeArray(array.length*2 + 1);
// Hole is generated at end of array
int hole = ++n;
System.out.println("H"+hole);
// Percolate up
for (array[0] = x; x < array[hole/2]; hole /= 2)
array[hole] = array[hole/2];
// Insert new element
array[hole] = x;
}
I get the NullPointerException
within the for loop from the insert method. Is it something with the way I'm dealing with the initially empty array?
Here's the initializing class:
public class BinaryHeap {
public static void main(String[] args)
{
int [] heapArray = {62, 75, 81, 71, 66, 69, 72, 73, 83, 82, 67, 72, 81, 73, 69, 90};
minHeap hp = new minHeap();
for(int i = 0; i < heapArray.length; i++)
{
hp.insert(heapArray[i]);
}
}
}