wood burning stoves 2.0*
The moose likes Java in General and the fly likes Linked List help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Linked List help" Watch "Linked List help" New topic
Author

Linked List help

Steve Shipman
Greenhorn

Joined: Mar 28, 2006
Posts: 27
****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

Joined: Aug 16, 2005
Posts: 14432
    
  23

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Steve Shipman
Greenhorn

Joined: Mar 28, 2006
Posts: 27
..

[ April 06, 2006: Message edited by: Steve Shipman ]
[ April 06, 2006: Message edited by: Steve Shipman ]
Steve Shipman
Greenhorn

Joined: Mar 28, 2006
Posts: 27
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

Joined: Jan 19, 2006
Posts: 55
How do you "println" a LinkedList?
You should tell the compiler what to print,
Like you did for Node.
Steve Shipman
Greenhorn

Joined: Mar 28, 2006
Posts: 27
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

Joined: Mar 28, 2006
Posts: 27
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

Joined: Jan 19, 2006
Posts: 55
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Linked List help