• Post Reply Bookmark Topic Watch Topic
  • New Topic

LinkedList append not working.  RSS feed

 
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am pretty much following a youtube tutorial on how to implement a linkedList(added the findNode method) but I get a nullpointer exception when I run the append method,This is the video I followed https://www.youtube.com/watch?v=njTh_OwMljA even when myself not being a very experienced programmer when I looked at this code I immediately noticed something up in the append method and it starts at the while loop after the while loop finishes current now equals null and straight after we assign current.next = new node but null does not have a new node? am I right and if not what is the problem?


thanks I'll leave the code under this
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
correction I meant to say null can not have a .next variable.
 
Sheriff
Posts: 4298
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moderation note: you don't have to quote the entire text from your previous post.  In fact we'd rather that you quote only what is needed to create the context of your reply.
 
Knute Snortum
Sheriff
Posts: 4298
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
after the while loop finishes current now equals null and straight after we assign current.next = new node 

That looks like a problem to me.  Have you tested it? 
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
after the while loop finishes current now equals null and straight after we assign current.next = new node 

That looks like a problem to me.  Have you tested it? 


the code compiles and runs ok as it should but an exception occurs

Exception in thread "main" java.lang.NullPointerException
at List.append(List.java:20)
at main.main(main.java:13)
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
after the while loop finishes current now equals null and straight after we assign current.next = new node 

That looks like a problem to me.  Have you tested it? 


the solution would be to test for if the next node is == null then break if it is so it allows you to add it to the end of the list



but yeah it baffles me that there is serveral comments on that youtube video and not one mentions that the code is invalid
 
Sheriff
Posts: 11498
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's an ugly solution. Your while loop should check (current.next != null) instead. No break necessary.
 
Ranch Hand
Posts: 86
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code in the tutorial looks correct, the shown while loop checks whether current.next is null, not whether current is null.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:That's an ugly solution. Your while loop should check (current.next != null) instead. No break necessary.


also I noticed with that solution or it could be with all the code in general when I added less than 2 nodes I would get an nullPointerException
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:
Junilu Lacar wrote:That's an ugly solution. Your while loop should check (current.next != null) instead. No break necessary.


also I noticed with that solution or it could be with all the code in general when I added less than 2 nodes I would get an nullPointerException


Yeah I got a piece of paper out and followed my solution yeah it flat out sucked haha
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!