Exception error for heap

Reply

Join Date: Nov 2007
Posts: 28
Reputation: Cleo123 is an unknown quantity at this point 
Solved Threads: 0
Cleo123 Cleo123 is offline Offline
Light Poster

Exception error for heap

 
0
  #1
Dec 1st, 2008
I'm not sure if I'm doing this correctly but up to now I get an exception error. Can you see why? :

  1.  
  2. public void countHeap(){
  3. System.out.println("input data:");
  4. for (int i = 0; i < dataSet.length; i++) {
  5. System.out.print(" "+dataSet[i]);
  6. count++;
  7. }
  8. noDataElements = count;
  9. formHeapTree();
  10. }
  11. public void formHeapTree(){
  12.  
  13. int unsortedSet[] = new int[noDataElements];
  14.  
  15. noDataElementsHalf = (noDataElements+1)/2;
  16.  
  17. System.out.println("\nheap Array");
  18. for (int j = 0; j < unsortedSet.length ; j++) {
  19. for (int i = noDataElementsHalf; i < noDataElements; i++) {
  20. unsortedSet[i] = dataSet[j];
  21. }
  22. System.out.print(" "+ unsortedSet[j]);
  23. }
  24. sortLevel();
  25.  
  26. for (int j = 0; j < unsortedSet.length ; j++) {
  27. for (int i = 0; i < noDataElementsHalf; i++) {
  28. unsortedSet[i] = dataSet[j];
  29. }
  30. System.out.print(" "+ unsortedSet[j]);
  31. }
  32. }
  33. public void sortLevel(){
  34.  
  35. System.out.print("\nheap array \n ");
  36. for (int i = unsortedSet.length; i > 0; i--) {
  37. int j = i-1;
  38. //Parent must be smaller than child
  39. if(unsortedSet[j] > unsortedSet[i]) { int temp = unsortedSet[i];
  40. unsortedSet[i] = unsortedSet[j];
  41. unsortedSet[j] = temp;
  42. }
  43. }
  44. }
Last edited by Cleo123; Dec 1st, 2008 at 2:11 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 822
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: Exception error for heap

 
0
  #2
Dec 1st, 2008
Please mention in detail what Exception you are getting.
BTW are you getting an ArrayOutOfBoundsException ?
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,176
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Exception error for heap

 
0
  #3
Dec 1st, 2008
Exception error, Now thats the most generic term I can think of when describing a problem, Can you be more specific as to what Exception you are getting and at what line number the exception is being thrown as indicated by your JVM ???
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 28
Reputation: Cleo123 is an unknown quantity at this point 
Solved Threads: 0
Cleo123 Cleo123 is offline Offline
Light Poster

Re: Exception error for heap

 
0
  #4
Dec 1st, 2008
Sorry...Null Pointer Exception - Exception in thread "main"
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 822
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: Exception error for heap

 
0
  #5
Dec 1st, 2008
Again you have posted the exception without much detail for e.g. line no. Also since oyu haven't put in your entire code and since also your code is not indented properly it is difficult figuring out where the exception would be and hence why it would be occurring at all.
Still I present a hint here : Nullpointer exception occur at places where you use a null reference. This would be where you are using the dot operator (.) and the reference on the LHS of it is a null reference. IF you can figure out yourself where this would be happening in your program - great, but if not you can help us by posting the code part in question e.g. the main method and the entire stack trace with line numbers for the code part.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 328
Reputation: quuba is on a distinguished road 
Solved Threads: 51
quuba quuba is offline Offline
Posting Whiz

Re: Exception error for heap

 
0
  #6
Dec 1st, 2008
public void formHeapTree() {

        int unsortedSet[] = new int[noDataElements];
Certainly int unsortedSet[] hides Your previous declaration -> delete int
quuba
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC