• Post Reply Bookmark Topic Watch Topic
  • New Topic

Delete method with linked lists  RSS feed

 
Greg Murphy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is this method throwing a null pointer exception when I call it from main?
I'm trying to submit an integer to indicate the number of nodes in a linked list to delete and then delete them headfirst.



Any help would be appreciated. Thanks.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by showing us the stack trace.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect head.getPrevious() returns null - in which case line 6 will throw a NPE.
 
Greg Murphy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I don't know what a strack trace is? Maybe if I show the rest of the code it will shed some more light.
Perhaps I'm setting up my linked list wrong? Should I be setting up the list with the constructor Node?
or is this correct to set it up creating new nodes in an insert?

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Murphy wrote:Ok I don't know what a strack trace is?


When you get a NullPointerException, you don't just get told it's a NullPointerException. You get told exactly what line it happens on. In fact, the error message will contain that line, and the line the method was called from, and the line that method was called from...all the way up to the top-level method. That's the stack trace.

So the JVM has told you exactly where the error has happened. It's much easier for people to help you if you pass on that information.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Murphy wrote:Ok I don't know what a strack trace is?


It's all the error messages on your console or in your log file after the error is displayed. It might look like this:

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!