Computer Science Canada Doubly Linked List Add method & Outputting problem |
Author: | EiresJason [ Fri Mar 08, 2013 7:41 pm ] |
Post subject: | Doubly Linked List Add method & Outputting problem |
Hi, i'm having problems with this program. Im trying to create an add method for a doublylikedlist class and trying to output certain data. Thanks for any sort of help. I keep getting a NullPointerException. Problems occur at the add method in the Iterator class; and also because of that; in the main class where i have "list.add(1, "A");" etc. IteratorList Class(Pretend its called DoublyLinkedLists); http://pastebin.com/ndMpuVxv Node Class: http://pastebin.com/GWDVNACS Main class; testing the methods. http://pastebin.com/jEnVHP8N |
Author: | Tony [ Fri Mar 08, 2013 7:59 pm ] |
Post subject: | RE:Doubly Linked List Add method & Outputting problem |
So what kind of problems occur? |
Author: | EiresJason [ Fri Mar 08, 2013 8:13 pm ] |
Post subject: | Re: Doubly Linked List Add method & Outputting problem |
This is the error i get from running it ![]() run: Exception in thread "main" java.lang.NullPointerException at jasonandrewsca3.IteratorList.add(IteratorList.java:139) at jasonandrewsca3.IteratorListDemo.main(IteratorListDemo.java:29) Java Result: 1 BUILD SUCCESSFUL (total time: 1 second) |
Author: | Sur_real [ Fri Mar 08, 2013 8:38 pm ] | ||||||
Post subject: | Re: Doubly Linked List Add method & Outputting problem | ||||||
This line:
I'm not sure but are you allowed to do this:
Should it not be:
|
Author: | EiresJason [ Fri Mar 08, 2013 8:53 pm ] |
Post subject: | Re: Doubly Linked List Add method & Outputting problem |
Oh ![]() I actually have <E> there 1min before i posted the thread ![]() Doesn't do anything and returns the same error as above. |
Author: | nullptr [ Fri Mar 08, 2013 8:57 pm ] |
Post subject: | Re: Doubly Linked List Add method & Outputting problem |
Maybe your size method is returning null? It looks like it might if the index is the same as the size of the list. |
Author: | Sur_real [ Fri Mar 08, 2013 9:21 pm ] | ||
Post subject: | RE:Doubly Linked List Add method & Outputting problem | ||
Awww...sorry then... yeah what nullptr said is true though, the size() method returns null then on line 140, you trying to do
but since cursor is null, you can't. |
Author: | EiresJason [ Fri Mar 08, 2013 10:08 pm ] |
Post subject: | Re: Doubly Linked List Add method & Outputting problem |
Okay thanks ![]() @Surreal Oh i see, thanks ![]() Is there anyway to fix it? I was literally doing the code from 6pm to 1am and it just made me angry and tried lol. I hope i can get it tomorrow; if any1 knows any way to fix it; please do let me know ![]() -------------------------------------------------------------------- @NullPTR Okay thanks ![]() -------------------------------------------------------------------- Thanks alot for the help. |
Author: | Tony [ Fri Mar 08, 2013 10:41 pm ] | ||||
Post subject: | RE:Doubly Linked List Add method & Outputting problem | ||||
"size" seems like a very misleading name for that method. You're not actually returning size, are you? What's likely happening is that the contents of your list are not what you are expecting them to be. So in your
print the value of cursor as you go along, to check that the elements are what you expect them to be. Also, what is the runtime of your toString() method? That is how many times do individual nodes get accessed, to print out a list of size N? Trust me, this is a very good exercise. Once you come up with a number, you can validate it (after you get your program to work) by printing something from
and counting how many times that method _actually_ gets called. |
Author: | EiresJason [ Fri Mar 08, 2013 11:51 pm ] |
Post subject: | Re: Doubly Linked List Add method & Outputting problem |
Okay thanks ![]() I just tried for another 1.5 hrs and i can't figure it out. Was suppose to hand the assignment in yesterday but that isn't an option anymore haha; might aswell try get as much as i can done before i hand it in. Thanks for the help. |
Author: | McKenzie [ Sat Mar 09, 2013 8:23 am ] |
Post subject: | Re: Doubly Linked List Add method & Outputting problem |
public IteratorList(Node current) - this method should probably have head and tail also point to current. public Object next() - This method crashes because hasNext() only checks if there is a node at the head, not after current. public void add(int index, E value) throws IndexOutOfBoundsException { - add dies when the list is empty size returns null (as it should despite the very odd name) then cursor.getNext() throws a nullPointerException |