• Post Reply Bookmark Topic Watch Topic
  • New Topic

using compareTo in bubbleUp method  RSS feed

 
Ehrick Messerle
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am working on an implementation of bubbleUp and here is my method:


I am having trouble adding an element at index 0 because the compareTo method keeps throwing a null pointer exception. Can someone help me with this? I did some debugging and the parentIndex going into the loop is 0, while the size is 1. Which makes sense.
 
Wayan Saryada
Ranch Hand
Posts: 119
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the error stacktrace so we know which line in your code producing the NPE error?
 
Abhishk Singh
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ehrick,
Welcome to the Ranch.
Can you post the code which initializes heap(array) along with the exception stacktrace?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ehrick Messerle wrote:I am having trouble adding an element at index 0 because the compareTo method keeps throwing a null pointer exception. Can someone help me with this? I did some debugging and the parentIndex going into the loop is 0, while the size is 1. Which makes sense.

NPEs always, always, always occur for the same reason: something in your program (in your case, almost certainly heap[parentIndex]) is null.

What's usually a lot harder to work out is why, because the underlying cause can often be a long way from where the error occurs.

My suggestion: Add
System.out.println(Arrays.toString(heap));
immediately before your while loop. It will show you exactly what's in your heap array (including nulls).

Then work from there.

Winston
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!