Paul Clapham wrote:Okay. I'm looking at that readList() method. Apparently that's supposed to be there for the purpose of reading from a text file and adding the lines from it into a list. As I already said, if it were me I would just skip the whole list business and create the JButtons right there. But at any rate that's all moot because you aren't actually calling that method anywhere.
Paul Clapham wrote:
What elements? My problem here is that I don't know what you mean by an "element".
My guess is that all you have to do is to get a String out of your list and assign it to the JButton using its setText() method, but the whole thing seems like an excessively complicated way of doing something which should be quite simple.
Paul Clapham wrote:But you don't even have any code there (in the first fragment) which tries to get data out of the nodes you created in the second fragment.
So is that your question? What code you should write to get data?
If you have a ProbNode object, then you just call its getName() method to get the string which was stored in the node. Are you asking how to get one of those? My guess would be that you have a ProbMgr object handy somewhere, and you can get the nodes from that. But that's just a guess, though. I don't know what this list is for or why you are using it. If it were me I would just read the text file and generate the buttons from that, rather than mess about with an intermediate step where you stash the data somewhere and then read it back.
Paul Clapham wrote:Well, there's a lot of code there. And all we know is that somewhere in that code, you have a null value appearing somehow and you don't think that should be happening. So could you be a little bit more specific about just where in that code you see the null value appearing?
Campbell Ritchie wrote:Is it a singly‑linked list? Sorry, I thought it was doubly‑linked. If it is singly‑linked, then you do not have the previous fields, nor do you have the tail reference. Only a head reference.
Advantage: you can reduce the number of steps in adding, removal, etc.
Disadvantage. You can only find nodes by going forwards from the head node (No 0) or the “current” node. I still think moving a node is better done by a combination of removal and adding. To move the No 14 node to position 10 when your “current” node is No 12:
1: Go forward 2 nodes to No 14. 2: Record that node temporarily. 3: Reset the “current” node back to the head node No 0. 4: Move 9 places to No 9. 5: Insert the previous No 14 node after No 9.You can see that finding a value in the List requires linear time, whereas finding a value in an array‑based List would take constant time (i.e. much faster).
Jeff Verdegan wrote:
In the OP he mentioned that is was a singly-linked list. If that changed, my bad for not noticing.
Jeff Verdegan wrote:
I don't do Swing, so I don't know if it makes sense to store a list of buttons. However, when you are ready to actually use a list "for real" (rather than just writing one for the educational exercise, which is a worthwhile task in its own right), be aware that the core API includes java.util.LinkedList and java.util.ArrayList. You'll want to use those rather than rolling your own, unless you need special functionality that they don't offer.
Tony Docherty wrote:Can you show the code you were trying to use