• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Linked List help

 
Steve Shipman
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
****Check latest post at bottom, ive came qutie a ways since I started, and code looks nothing like this anymore*****

Im trying to create a Linked List of 6,000 odd elements, this is what I have down



Ok, so im pretty horrible at LinkedLists so im not too sure what I need to do from here. Im not sure if the code I have will work to create the list to 6000, the instructor helped us with the populate method, but class ended so it wasnt much help. So I created the main to try and test it, and im not really sure what to use to call the method "populateList" in main, so I used head. When I do this I get:

"Exception in thread "main" java.lang.NullPointerException
at LuckyNumbers.populateList(LuckyNumbers.java:25)
for the line "head.item = 1;"
at LuckyNumbers.main(LuckyNumbers.java:51)"
for the line "populateList(head);"



like I said not sure if my code is correct to populate a linked list of 6,000 numbers, but thats what ive got so far. Any help would be greatly appreciated! Thanks in advance!

[ April 06, 2006: Message edited by: Steve Shipman ]
[ April 06, 2006: Message edited by: Steve Shipman ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the line "head.item = 1", "head" is still null. You need to create a Node object first with the "new" operator. There might be other issues with your code, but I didn't look at it very closely...

You are probably doing this as an assignment? In a real world Java program, you wouldn't be implementing your own linked list, since the standard Java API already contains a linked list implementation.
 
Steve Shipman
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
..

[ April 06, 2006: Message edited by: Steve Shipman ]
[ April 06, 2006: Message edited by: Steve Shipman ]
 
Steve Shipman
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I have came quite a ways since I posted this last night, this is what I have now, its looking pretty good but somthing is still wrong



Looks good, compiles without any errors, however....nothing prints, I included the println saying "Done" to see if it got to there, and it does, so the program works, but I dont see the list. I checked the populateList method, it calls the add method correctly, so my guess is somthing is wrong with my add method, but I cant figure it out. Thanks guys
 
fred Joly
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you "println" a LinkedList?
You should tell the compiler what to print,
Like you did for Node.
 
Steve Shipman
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so are you saying I need a toString method along with my add method in the linkedList class??



same as my node class, except obj is used instead of item? Not sure if this is what you mean or not
[ April 06, 2006: Message edited by: Steve Shipman ]
 
Steve Shipman
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmmm for my linkedlist constructor I changed


to


now when I run it I get.....
"LinkedList@360be0
Done"

so your right I cant use the statement "System.out.println(list);", but I dont know what to use instead, and im pretty sure that toString I have above, is NOT the right way
 
fred Joly
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess that what you want to do is to print every node of your LinkedList.
First thing : try to print only one Node, not a LinkedList containing one
node, just a Node by itself.(try to figure out how toString() is working)
After that, all you have to do is to print all the nodes of your list one by one.

But in order to do this, you need to be able to access the Nodes of your list....maybe a next() method in the linkedList class....




so are you saying I need a toString method along with my add method in the linkedList class?


If you want to be able to just write

and get something a human can read , yes you need a toString method.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic