Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Doubly Linked List Add method & Outputting problem
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
EiresJason




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Fri Mar 08, 2013 7:59 pm   Post subject: RE:Doubly Linked List Add method & Outputting problem

So what kind of problems occur?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
EiresJason




PostPosted: 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 Very Happy(forgot to add this lol)

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)
Sur_real




PostPosted: Fri Mar 08, 2013 8:38 pm   Post subject: Re: Doubly Linked List Add method & Outputting problem

This line:
Java:
Node<E> temp = new Node<>(value);

I'm not sure but are you allowed to do this:
Java:
new Node<>(value)

Should it not be:
Java:
Node<E> temp = new Node<E>(value);
EiresJason




PostPosted: Fri Mar 08, 2013 8:53 pm   Post subject: Re: Doubly Linked List Add method & Outputting problem

Oh Very Happy

I actually have <E> there 1min before i posted the thread Sad

Doesn't do anything and returns the same error as above.
nullptr




PostPosted: 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.
Sur_real




PostPosted: 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
Java:
cursor.getNext()

but since cursor is null, you can't.
EiresJason




PostPosted: Fri Mar 08, 2013 10:08 pm   Post subject: Re: Doubly Linked List Add method & Outputting problem

Okay thanks Very Happy

@Surreal
Oh i see, thanks Very Happy
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 Very Happy
--------------------------------------------------------------------

@NullPTR
Okay thanks Very Happy I do think it's the cursor problem though.
--------------------------------------------------------------------

Thanks alot for the help.
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: 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
code:

for (int i = 0; i < index; i++) {
                        cursor = cursor.getNext();
                }

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
code:

Node<E> getNext() {
        return next;
    }

and counting how many times that method _actually_ gets called.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
EiresJason




PostPosted: Fri Mar 08, 2013 11:51 pm   Post subject: Re: Doubly Linked List Add method & Outputting problem

Okay thanks Very Happy

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.
McKenzie




PostPosted: 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
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: