3 Replies - 686 Views - Last Post: 28 October 2012 - 06:01 PM Rate Topic: -----

#1 NiftyClown  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-October 12

Null Pointer Exception - Assigning value to an array

Posted 28 October 2012 - 05:49 PM

Hi there,

I've been writing a program which implements the Queue interface using an extendable circular array. However, I'm getting an extremely strange error when assigning a value to my array. I can't figure out why it is doing so because to me, the code looks all right. I've tried to find solutions on the web but to no avail.

Here is my code:

public class CircularArray<E> implements QueueADT<E> {

	E[] arr; // The array to store the queue elements
	private int cap = 5; // The initial capacity of the array
	private int front; // The front of the queue
	private int rear; // The rear of the queue
	private int num; // The number of elements in the array
	
	/*
	 * The constructor for this class.
	 * It will initialise and create the array and 
	 * elements.
	 */
	public CircularArray() {
		E[] arr = (E[])new Object[cap]; // Creates the array with the initial capcity.
		front = 0; // Initialises the front
		rear = 0;  // and rear of the queue to zero
		num = 0;
	}
	
	/* Adds an element to the rear of the queue */
	public void enqueue(E element) {		
		// Checks to see if the array has reached capacity
		if (num == cap-1){
			cap = cap + 5; // Increases the capacity
			E[] temp = (E[])new Object[cap]; // Temporary array to store values
			for (int i = 0; i<arr.length; i++){
				arr[i] = temp[i]; // Assigns the values in the queue to temp array
			}
			E[]arr = (E[])new Object[cap]; // Increases capacity of current array
			arr = temp; // Re-assigns the values
		}
		
		arr[rear] = element; // Implements the element in the array
		rear = (rear++) % cap; // Increments the rear count.
		num++;
	}

	/* Removes the element at the front of the queue */
	public E dequeue() throws EmptyQueueException {
		E element; // Declares the generic element for returning.
		
		// Checks to see if the queue is empty
		if(isEmpty()){
			throw new EmptyQueueException(""); // Throws an exception if so.
		} else 
			element = arr[front]; // Assigns the element to the front element in the queue
			arr[front] = null; // Nullifies the front element
			front = (front++) % cap; // Checks if the count has reached the capacity
			num--;
			return element;
	}

	/* Checks the element at the front of the queue and returns it */
	public E front() throws EmptyQueueException {
		E element; // Declares the generic element for returning.
		
		// Checks to see if the queue is empty
		if(isEmpty()){
			throw new EmptyQueueException(""); // Throws an exception if so.
		} else 
			element = arr[front]; // Assigns the element to the front element in the queue
			return element; // Returns the element at the front.
	}

	/* Returns the size of the queue */
	public int size() {
		return num;
	}

	/* Checks to see if the queue is empty */
	public boolean isEmpty() {
		return(front == rear);
	}
		
}



And the error I received is:


Exception in thread "main" java.lang.NullPointerException
at CircularArray.enqueue(CircularArray.java:46)
at Driver.main(Driver.java:50)


Would really appreciate any help I could get on this conundrum.

Thanks :)

      arr[rear] = element; // Implements the element in the array 


This is the line which is throwing the error

Is This A Good Question/Topic? 0
  • +

Replies To: Null Pointer Exception - Assigning value to an array

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7292
  • View blog
  • Posts: 12,097
  • Joined: 19-March 11

Re: Null Pointer Exception - Assigning value to an array

Posted 28 October 2012 - 05:54 PM

E[] arr = (E[])new Object[cap];


This line in the constructor is you problem. When you initialize here, you're declaring and initializing a local array that is also called "arr". This is a different array from the one you declare at the head of the file. If you remove the declaration part of this line, it should work fine, assuming "rear" is within the bounds of that array.
Was This Post Helpful? 1
  • +
  • -

#3 NiftyClown  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-October 12

Re: Null Pointer Exception - Assigning value to an array

Posted 28 October 2012 - 06:00 PM

Jeez, can't believe I missed that!
Haha, oh well!

Thanks for taking the time to answer me.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7292
  • View blog
  • Posts: 12,097
  • Joined: 19-March 11

Re: Null Pointer Exception - Assigning value to an array

Posted 28 October 2012 - 06:01 PM

Glad to help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1