Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Deque and Queues: Exception in thread "main" java.lang.NullPointerException Error

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Deque and Queues: Exception in thread "main" java.lang.NullPointerException Error

    Hello everyone, so I have this assignment where I have to create a Deque class and a Queue class and to be more efficient my professor wants me to create the deque class for then use the methods made in the class to implement the queue. He says: "To implement the Stack and Queue classes, use the Deque class to do the work. You may not create any arrays or other complex objects within the methods of the Stack or Queue classes. Create an instance of the Dequeue class in the constructor and use the methods of the Dequeue to provide the appropriate functionality of the other methods of the Stack and Queue classes." Now for the Queue class we must use the circular array implementations strategy. This is how my code looks like right now:

    class Deque {
    	private Object[] deque;
    	private final int MAX_SIZE = 5;
    	private int size;
     
    	public Deque() {
    		deque = new Object[MAX_SIZE];
    		size = 0;
    	}
     
    	public void insertOnFront(Object item) {
    		if (isEmpty()) {
    			deque[0] = item;
    			size++;
    		}
    		else {
    			Object[] temp;
     
    			if (size == deque.length) {
    				temp = new Object[size * 2];
     
    				temp[0] = item;
     
    				for (int i = 0; i < size; i++) {					
    					temp[i + 1] = deque[i];
    				}
    			}
    			else {
    				temp = new Object[deque.length];
     
    				temp[0] = item;
     
    				for (int i = 0; i < size; i++) {
    					temp[i + 1] = deque[i];
    				}
    			}
     
    			deque = temp;
    			size++;
    		}
    	}
     
    	public Object deleteFromFront() {
    		Object itemDeleted;
     
    		if (isEmpty()) {
    			itemDeleted = null;
    		}
    		else {
    			itemDeleted = deque[0];
     
    			for (int i = 0; i < size; i++) {
    				deque[i] = deque[i + 1];
    			}
    			size--;
    		}
     
    		return itemDeleted;
    	}
     
    	public void insertOnBack(Object item) {
    		if (isEmpty()) {
    			deque[0] = item;
    			size++;
    		}
    		else {
    			Object[] temp;
     
    			if (size == deque.length) {
    				temp = new Object[size * 2];
     
    				for (int i = 0; i < size; i++) {
    					temp[i] = deque[i];
    				}
     
    				temp[size] = item;	
    			}
    			else {
    				temp = new Object[deque.length];
     
    				for (int i = 0; i < size; i++) {
    					temp[i] = deque[i];
    				}
     
    				temp[size] = item;
    			}
    			deque = temp;
    			size++;
    		}
    	}
     
    	public Object deleteFromBack() {
    		Object itemDeleted;
     
    		if (isEmpty()) {
    			itemDeleted = null;
    		}
    		else {
    			itemDeleted = deque[size - 1];
     
    			deque[size - 1] = null;
     
    			size--;
    		}
     
    		return itemDeleted;
    	}
     
    	private boolean isEmpty() {
    		return (size == 0);
    	}
     
    	public String toString() {
    		String s = null;
     
    		for (int i = 0; i < size; i++) {
    			s = deque[i] + "\n";
    		}
     
    		return s;
    	}
     
    	public void toStore() {
     
    	}
    }
     
    class Queue {
    	Deque[] queue;
    	int front, rear, count;
    	private final int MAX_SIZE = 5;
     
    	public Queue() {
    		queue = new Deque[MAX_SIZE];
    		front = 0;
    		rear = 0;
    		count = 0;
    	}
     
    	public void insert(Object item) {
    		Deque[] temp;
     
    		if (count == queue.length) {
    			temp = new Deque[queue.length * 2];
     
    			for (int i = 0; i < count; i++) {
    				temp[i] = queue[front];
    				front = (front + 1) % queue.length;
    			}
     
    			front = 0;
    			rear = count;
    			queue = temp;
    		}
     
    		queue[rear].insertOnBack(item);
    		rear = (rear + 1) % queue.length;
    		count++;
    	}
     
    	public Object delete() {		
    		if (isEmpty()) {
    			return null;
    		}
    		else {
    			Object result = queue[front];
    			queue[front] = null;
     
    			front = (front + 1) % queue.length;
     
    			count--;
     
    			return result;
    		}
    	}
     
    	public boolean isEmpty() {
    		return (count == 0);
    	}
     
    	public String toString() {
    		String result = "";
    		int scan = 0;
     
    		while (scan < count) {
    			if (queue[scan] != null) {
    				result += queue[scan].toString() + "/n";
    			}
    			scan++;
    		}
     
    		return result;
    	}
    }

    This is my main:

    class program4 {
     
    	public static void main(String[] args) {
    		Queue test = new Queue();
     
    		test.insert(5);
    		test.insert(6);
    		test.insert(7);
    		test.insert(8);
    		test.insert(9);
    		test.insert(10);
    		test.insert(11);
    		test.insert(12);
    		test.delete();
    		test.delete();
    	}
     
    }

    When I run my code I receive this error:

    Exception in thread "main" java.lang.NullPointerException
    at Queue.insert(program4.java:180)
    at program4.main(program4.java:249)


    I go into debugging and when I click on my queue that I create is see this error also:

    org.eclipse.debug.core.DebugException: com.sun.jdi.ClassNotLoadedException: Type has not been loaded occurred while retrieving component type of array.

    not too sure where to go from here so I am a little bit confused and any help will be appreciated. Thanks in advance


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,910
    Thanks
    55
    Thanked 2,351 Times in 2,322 Posts

    Default Re: Deque and Queues: Exception in thread "main" java.lang.NullPointerException Error

    Exception in thread "main" java.lang.NullPointerException
    at Queue.insert(program4.java:180)
    Look at line 180 and find the variable with the null value. Then backtrack to see why that variable is null.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Deque and Queues: Exception in thread "main" java.lang.NullPointerException Error

    well I put the extends Deque on my Queue class like so:

    class Queue extends Deque{
    	Deque[] queue;
    	int front, rear, count;
    	private final int MAX_SIZE = 5;
     
    	public Queue() {
    		queue = new Deque[MAX_SIZE];
    		front = 0;
    		rear = 0;
    		count = 0;
    	}
     
    	public void insert(Object item) {
    		Deque[] temp;
     
    		if (count == queue.length) {
    			temp = new Deque[queue.length * 2];
     
    			for (int i = 0; i < count; i++) {
    				temp[i] = queue[front];
    				front = (front + 1) % queue.length;
    			}
     
    			front = 0;
    			rear = count;
    			queue = temp;
    		}
     
    		queue[rear].insertOnBack(item);
    		rear = (rear + 1) % queue.length;
    		count++;
    	}
     
    	public Object delete() {		
    		if (isEmpty()) {
    			return null;
    		}
    		else {
    			Object result = queue[front];
    			queue[front] = null;
     
    			front = (front + 1) % queue.length;
     
    			count--;
     
    			return result;
    		}
    	}
     
    	public boolean isEmpty() {
    		return (count == 0);
    	}
     
    	public String toString() {
    		String result = "";
    		int scan = 0;
     
    		while (scan < count) {
    			if (queue[scan] != null) {
    				result += queue[scan].toString() + "/n";
    			}
    			scan++;
    		}
     
    		return result;
    	}
    }

    and when I am under debugging I see two arrays that have been initialized and that is the deque array when I initialize my queue a long with my queue array and both of them have null for the five elements that is set. So I know where the null is at just not sure how to fix it

    and line 180 is when I am trying to put the first element into my array

    queue[rear].insertOnBack(item);

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,910
    Thanks
    55
    Thanked 2,351 Times in 2,322 Posts

    Default Re: Deque and Queues: Exception in thread "main" java.lang.NullPointerException Error

    What variable on line 180 is null?

    Why doesn't that variable have a valid value?

    not sure how to fix it
    I have no idea what the logic of the code is. Normally that is explained in comments, but this code has no comments so I can't say if the code is doing what it is supposed to do or not. Or if the logic makes sense.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Deque and Queues: Exception in thread "main" java.lang.NullPointerException Error

    I fixed it.. Just had to call the methods for the Deque class and set the deque in the Deque class to public and the make queue array equal to the deque array

    class Deque {
    	public Object[] deque;
    	private final int MAX_SIZE = 5;
    	private int size;
     
    	public Deque() {
    		deque = new Object[MAX_SIZE];
    		size = 0;
    	}
     
    	public void insertOnFront(Object item) {
    		if (isEmpty()) {
    			deque[0] = item;
    			size++;
    		}
    		else {
    			Object[] temp;
     
    			if (size == deque.length) {
    				temp = new Object[size * 2];
     
    				temp[0] = item;
     
    				for (int i = 0; i < size; i++) {					
    					temp[i + 1] = deque[i];
    				}
    			}
    			else {
    				temp = new Object[deque.length];
     
    				temp[0] = item;
     
    				for (int i = 0; i < size; i++) {
    					temp[i + 1] = deque[i];
    				}
    			}
     
    			deque = temp;
    			size++;
    		}
    	}
     
    	public Object deleteFromFront() {
    		Object itemDeleted;
     
    		if (isEmpty()) {
    			itemDeleted = null;
    		}
    		else {
    			itemDeleted = deque[0];
     
    			for (int i = 0; i < size; i++) {
    				deque[i] = deque[i + 1];
    			}
    			size--;
    		}
     
    		return itemDeleted;
    	}
     
    	public void insertOnBack(Object item) {
    		if (isEmpty()) {
    			deque[0] = item;
    			size++;
    		}
    		else {
    			Object[] temp;
     
    			if (size == deque.length) {
    				temp = new Object[size * 2];
     
    				for (int i = 0; i < size; i++) {
    					temp[i] = deque[i];
    				}
     
    				temp[size] = item;	
    			}
    			else {
    				temp = new Object[deque.length];
     
    				for (int i = 0; i < size; i++) {
    					temp[i] = deque[i];
    				}
     
    				temp[size] = item;
    			}
    			deque = temp;
    			size++;
    		}
    	}
     
    	public Object deleteFromBack() {
    		Object itemDeleted;
     
    		if (isEmpty()) {
    			itemDeleted = null;
    		}
    		else {
    			itemDeleted = deque[size - 1];
     
    			deque[size - 1] = null;
     
    			size--;
    		}
     
    		return itemDeleted;
    	}
     
    	private boolean isEmpty() {
    		return (size == 0);
    	}
     
    	public String toString() {
    		String s = null;
     
    		for (int i = 0; i < size; i++) {
    			s = deque[i] + "\n";
    		}
     
    		return s;
    	}
     
    	public void toStore() {
     
    	}
    }
     
    class Queue extends Deque{
    	Object[] queue;
    	int front, rear, count;
    	private final int MAX_SIZE = 5;
     
    	public Queue() {
    		queue = new Object[MAX_SIZE];
    		front = 0;
    		rear = 0;
    		count = 0;
    	}
     
    	public void insert(Object item) {
    		Object[] temp;
     
    		if (count == queue.length) {
    			temp = new Object[queue.length * 2];
     
    			for (int i = 0; i < count; i++) {
    				temp[i] = queue[front];
    				front = (front + 1) % queue.length;
    			}
     
    			front = 0;
    			rear = count;
    			queue = temp;
    		}
     
    		insertOnBack(item);
    		queue[rear] = deque[count];
    		rear = (rear + 1) % queue.length;
    		count++;
    	}
     
    	public Object delete() {		
    		if (isEmpty()) {
    			return null;
    		}
    		else {
    			Object result = queue[front];
    			queue[front] = null;
     
    			deleteFromFront();
     
    			front = (front + 1) % queue.length;
     
    			count--;
     
    			return result;
    		}
    	}
     
    	public boolean isEmpty() {
    		return (count == 0);
    	}
     
    	public String toString() {
    		String result = "";
    		int scan = 0;
     
    		while (scan < count) {
    			if (queue[scan] != null) {
    				result += queue[scan].toString() + "/n";
    			}
    			scan++;
    		}
     
    		return result;
    	}
    }

Similar Threads

  1. I get the error Exception in thread "main" java.lang.NullPointerException?
    By jeremy28 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 4th, 2013, 12:13 AM
  2. Replies: 1
    Last Post: April 7th, 2013, 04:40 PM
  3. Replies: 1
    Last Post: February 27th, 2013, 06:48 AM
  4. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  5. [SOLVED] Error Message: Exception in thread "main" java.lang.NullPointerException etc.
    By coffecupcake in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 15th, 2011, 09:34 AM