473,410 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

Null Pointer Exception

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
Sep 20 '09 #1
1 1637
r035198x
13,262 8TB
Please use code tags if you have to post code.
Read this article.
Sep 20 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

102
by: junky_fellow | last post by:
Can 0x0 be a valid virtual address in the address space of an application ? If it is valid, then the location pointed by a NULL pointer is also valid and application should not receive "SIGSEGV"...
5
by: Boniek | last post by:
Hi I define a public property in a new form and I can see this property in table of Properties in Visual. How I can hide this property to see only in code ? Thank's Boniek
5
by: Tony Cheng | last post by:
for (int i=0; i<_request.Form.Count; i++ ) { string key = _request.Form.GetKey(i); if ( !key.Equals("formCode") && !key.Equals("formLanguage") && !key.Equals("__VIEWSTATE") &&...
69
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after...
18
by: Denis Petronenko | last post by:
Hello, in the following code i have segmentaion fault instead of exception. Why? What i must to do to catch exceptions in such situation? Used compiler: gcc version 3.3.6 (Debian 1:3.3.6-13) ...
3
by: Michael | last post by:
Hi, Could you tell me how to under the following statements? Does it mean each and every memory allocation will check all the pointer no matter a null pointer or not? Right? Thanks in advance! ...
1
by: zahidkhan | last post by:
Hi All, Plz help me if you can..... I have a program something like this int main(int argc,char* argv) { try { int* p = NULL;
46
by: lovecreatesbea... | last post by:
Do you prefer malloc or calloc? p = malloc(size); Which of the following two is right to get same storage same as the above call? p = calloc(1, size); p = calloc(size, 1);
76
by: valentin tihomirov | last post by:
As explained in "Using pointers vs. references" http://groups.google.ee/group/borland.public.delphi.objectpascal/browse_thread/thread/683c30f161fc1e9c/ab294c7b02e8faca#ab294c7b02e8faca , the...
18
by: sanjay | last post by:
Hi, I have a doubt about passing values to a function accepting string. ====================================== #include <iostream> using namespace std; int main() {
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.