Connecting Tech Pros Worldwide Help | Site Map

Null Pointer Exception

Newbie
 
Join Date: Sep 2009
Posts: 1
#1: Sep 20 '09
I have to write two new classes into a Queue class that implements through an array. It seems so simple and I'm almost completely positive that all the main componets are there. However, when trying to test it anything that I have altered in the classes SwitchEnds and in Equalize comes back with a java.lang.NullPointerException. I am at my breaking point as it seems I have used the same methods used in the Enqueue and Dequeue methods(which work flawlessly). I can't understand this at all and any help will be greatly appreciated.

The class-
Expand|Select|Wrap|Line Numbers
  1. public class Queue<QueueItem>
  2. {
  3. private Object[] Array;
  4. private int Front, Rear, QueueMax;
  5. private Object QueueItem;
  6.  
  7. public Queue(int Capacity)
  8. {
  9. Array = new Object[Capacity+1];
  10. QueueMax = Capacity;
  11. Front = 0;
  12. Rear = 0;
  13. return;
  14. }
  15. // end constructor
  16.  
  17. public Queue()
  18. {
  19. Array = new Object[1001];
  20. QueueMax = 1000;
  21. Front = 0;
  22. Rear = 0;
  23. return;
  24. }
  25. // end constructor
  26.  
  27. public void Enqueue(QueueItem QI)
  28. {
  29. Array[Rear] = QI;
  30.  
  31. if (Rear == QueueMax)
  32. Rear = 0;
  33. else
  34. Rear++;
  35. // end if
  36.  
  37. if (Front == Rear)
  38. {
  39. System.out.println("Queue Overflow");
  40. System.exit(0);
  41. }
  42. // end if
  43. return;
  44. }
  45. // end public method Enqueue
  46.  
  47. public QueueItem Dequeue()
  48. {
  49. QueueItem QI;
  50.  
  51. if (Front == Rear)
  52. {
  53. System.out.println("Queue Underflow!");
  54. System.exit(0);
  55. }
  56. // end if
  57.  
  58. QI = (QueueItem) Array[Front];
  59. if (Front == QueueMax)
  60. Front = 0;
  61. else
  62. Front++;
  63. // end if
  64.  
  65. return QI;
  66. }
  67. // end public method Dequeue
  68.  
  69. public void MakeEmpty()
  70. {
  71. Front = 0;
  72. Rear = 0;
  73. return;
  74. }
  75. // end public method MakeEmpty
  76.  
  77. public boolean IsEmpty()
  78. {
  79. if (Front == Rear)
  80. return true;
  81. else
  82. return false;
  83. // end if
  84. }
  85. // end public method IsEmpty
  86.  
  87. public boolean IsFull()
  88. {
  89. int F=Front, R=Rear+1;
  90.  
  91. if (R == QueueMax+1)
  92. R = 0;
  93. // end if
  94.  
  95. if (F == R)
  96. return true;
  97. else
  98. return false;
  99. // end if
  100. }
  101. // end public method IsFull
  102. public void SwitchEnds()
  103. {
  104. if(this.LengthQueue() <= 1)
  105. return;
  106.  
  107. Object QI, QI2;
  108. QI = (QueueItem) Array[Rear];
  109. QI2 = (QueueItem) Array[Front];
  110. Array[Rear] = QI2;
  111. Array[Front] = QI;
  112. return;
  113. }
  114.  
  115. public void Equalize(Queue<QueueItem> Q)
  116. {
  117. QueueItem Temp;
  118. int Length1, Length2, NumberLeftInLongerQueue;
  119.  
  120. Length1 = this.LengthQueue();
  121. Length2 = Q.LengthQueue();
  122.  
  123. if (Length1 < Length2)
  124. {
  125. Q.Equalize(this);
  126. return;
  127. }
  128.  
  129. if (Length1 - Length2 <= 1)
  130. return;
  131.  
  132. NumberLeftInLongerQueue = (Length1 - Length2) / 2;
  133. if((NumberLeftInLongerQueue + Q.LengthQueue()) > Q.QueueMax)
  134. {
  135. System.out.println("The smaller queue does not have a large enough capacity.");
  136. System.exit(0);
  137. }
  138.  
  139. for (int i = 1; i <= NumberLeftInLongerQueue; i++)
  140. {
  141. Temp = (QueueItem) Array[Rear];
  142. if(Rear == 0 )
  143. Rear = QueueMax;
  144. else
  145. Rear = Rear -1;
  146.  
  147. Q.Enqueue(Temp);
  148.  
  149. }
  150.  
  151. return;
  152.  
  153. }
  154. public int LengthQueue()
  155. {
  156. if (Front <= Rear)
  157. return Rear - Front;
  158. else
  159. return QueueMax - Front + Rear + 1;
  160. // end while
  161. }
  162. // end public method LengthQueue
  163. }
  164. // end class Queue
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Sep 20 '09

re: Null Pointer Exception


Please use code tags if you have to post code.
Read this article.
Reply