• Post Reply Bookmark Topic Watch Topic
  • New Topic

deleting from a Linked List  RSS feed

 
Hannah Sigmon
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are supposed to be creating our own Linked List class and they are confusing me to death. The linked list is supposed to contain objects of the class Item, which is a private inner class within the Linked List class. Each object of Item has a double variable called price, and I'm trying to the value of price of each object in the Linked List, and if that value is greater than another double (called bankAccount), then I want that object to be deleted. For what I have now, everything seems to run fine except the delete method. When an object has to be deleted, this is the message I get: Exception in thread "main" java.lang.NullPointerException
at StringLinkedList$Item.access$0(StringLinkedList.java:22)
at StringLinkedList.deleteCurrentNode(StringLinkedList.java:268)
at StringLinkedList.goShopping(StringLinkedList.java:286)
at StringLinkedListDemo.main(StringLinkedListDemo.java:52)

I really have no idea how to fix this. Any suggestions?

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hannah Sigmon wrote:
Exception in thread "main" java.lang.NullPointerException
at StringLinkedList$Item.access$0(StringLinkedList.java:22)
at StringLinkedList.deleteCurrentNode(StringLinkedList.java:268)
at StringLinkedList.goShopping(StringLinkedList.java:286)
at StringLinkedListDemo.main(StringLinkedListDemo.java:52)

I really have no idea how to fix this. Any suggestions?


Since the error message is telling you that you're dereferencing a null pointer on line 22 of StringLinkedList.java (as well as telling you which methods got called to get you to that line of code), I would start by looking at that line and figuring out which references it could possibly be talking about. Then you need to review your code to figure out which one is null, and why. If you can't tell just be looking at your code, you'll want to add a bunch of println() calls to show what's happening at each step of the way, what relevant variables' values are, which code paths are being taken.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Hannah Sigmon
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I fixed it to where it is no longer giving me the NullException, but now instead of deleting only the items that have a price variable greater than bankAccount, it is just deleting everything, or so it seems. I have a method called writeOutput that just prints all the items in the linked list, and when I call it outside this method here, it works fine. But either it is messed up inside this method or the deleteCurrentNode method doesn't work, because the code doesn't output any items. This is what I have now:



And this is what I get back (these items and their attributes have already been input by other methods):


Item name: hat
Item quantity: 4
Item price: $17.15
Item priority: 1

Item name: shoes
Item quantity: 2 pairs
Item price: $18.8
Item priority: 2

Item name: boots
Item quantity: 3 pairs
Item price: $24.58
Item priority: 3

How much money do you have to spend (in dollars)?
40

You bought these items:

You started with this much money: $40.0

You have this much money left over: $4.05

when instead it should say:

You bought these items:

Item name: hat
Item quantity: 4
Item price: $17.15
Item priority: 1

Item name: shoes
Item quantity: 2 pairs
Item price: $18.8
Item priority: 2

You started with this much money: $40.0

You have this much money left over: $4.05

And the boots should have been deleted because they were too expensive.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Time for drastic action, I am afraid.
Search this forum for linked list. You will see all sorts of threads about it, and people who designed their own linked lists. See how they did it. See what problems they had.
Draw diagrams of how linked lists work. Find out about head nodes and tail nodes, in particular which of them has a reference pointing to null. Many linked list implementations have a null somewhere and you can never quite get rid of it. Or more precisely, if you get rid of one null, another appears somewhere else.
I hope that will teach you how linked lists work, and allow you to work out the algorithm for removing a node. You have correctly worked out that it involves moving references in adjacent nodes, but I think you need to check very carefully which reference needs to be kept and which discarded.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!