I have to use a queue in an solitaire assignment, but I'm having some trouble using the linked list Queue class that I made. It won't take arrays at all.

class Testing {
	public static void main (String [] args) {
		Queue testlist = new Queue(); // testing
		Queue [] testlistarray = new Queue [5000];

		testlist.add("aa");
		testlist.add("bb");
		testlist.add("cc");
		System.out.println(testlist.remove());
		System.out.println(testlist.remove());
		System.out.println(testlist.remove());

		testlistarray[0].add("dd");
		testlistarray[0].add("ee");
		testlistarray[0].add("ff");
		System.out.println(testlistarray[0].remove());
		System.out.println(testlistarray[0].remove());
		System.out.println(testlistarray[0].remove());

	}
}



class Queue {
	private int queuesize; // queue size
	private Node first; // first in queue
	private Node last; // last in queue

	private class Node {
		private Object data; // String, int, etc.
		private Node next; // link to next
	}

	public Queue() { // create empty queue
		first = null;
		last = null;
	}

	public int size() { return queuesize; } // return current queue size

	public boolean isempty() { return first == null; } // check if queue is empty

	public void add(Object data) { // add object to queue
		Node a = new Node();
		a.data = data;
		if (isempty()) { first = a; last = a;}
		else { last.next = a; last = a;}
		queuesize++;
	}

	public Object remove() { // remove earliest object in queue and return data
		Object data = first.data;
		first = first.next;
		queuesize--;
		return data;
	}
}

It compiles just fine, but when running it I get:
aa
bb
cc
Exception in thread "main" java.lang.NullPointerException
at Testing.main(Testing.java:13)

I find it very strange, since testlist and testlistarray[] are essentially the same.

What am I doing wrong? I've been trying different things for over an hour, but I'm really just treading water at this point.

I'll try to look at it with a clear head in the morning. Hoping for some enlightening replies.

Thanks for reading :)

public static void main (String [] args) {
		Queue testlist = new Queue(); // testing
		Queue [] testlistarray = new Queue [5000];

For all I know, an array declaration in Java looks more like this: type array_name [] = new type [alloc_size]; I don't really know what putting the []'s between the type and the name does, but it looks like it might be a valid syntax, though not the one you are looking for. Try changing that and you might see better results!

Emil Olofsson

For all I know, an array declaration in Java looks more like this: type array_name [] = new type [alloc_size]; I don't really know what putting the []'s between the type and the name does, but it looks like it might be a valid syntax, though not the one you are looking for. Try changing that and you might see better results!

Emil Olofsson

Thanks for the quick reply. I was wondering if maybe I had a syntax bug somewhere. I tried moving the []'s, but I still encounter the same error. I'm not really sure what the difference is between the placements of [], but it didn't seem to have any effect in this case.

Currently I'm leaning more towards a flaw in the Queue class design.

Signing off for today. Will continue tomorrow.:zzz:

for (int i=0; i < testlistarray.length; i++) {
			testlistarray[i] = new Queue();
		}

The array declaration sets aside memory of Queue objects but they remain uninitialize.

Hey, I think I see your problem, but someone else has already gotten the credit for solving your thread! The problem isn't in the Queue class, but in the Testing classes main method. When you use an object as a data type like you did ( the testlistarray ) you don't actually create an instance of the class. What you create is a reference that points to nothing, that is what the NullPointerException is telling you; you forgot to initialize the array or in other words you forgot to actually create the objects! There isn't anything for you to add a value to.

I know that someone else has given you the few lines of code to fix the error, but I'm hoping that I could get some credit for an explanation? If you didn't already know this that is.

I don't mind sharing the credit (I don't even know how that works) But in addition to the code I did provide explanation (succinct as it was) for the NullPointerException.

What's this talk about credit? Can I give you 'credit points' or something? I usually just + vote every post that helped me and mark the thread as solved.

The solution worked like a charm and the explanation helped me understand why. So thank you both :*

P.S. Someone seems to have downvoted your post rue64ja. so you're stuck at 0 after my +.

Edited 6 Years Ago by Yeen: n/a

I did not know what those arrows were for....now I know! I up voted the user that provided detailed explanation.

This question has already been answered. Start a new discussion instead.