This is the line giving the error:
node.setPrevious(node.setNext(new DLLNode("Two",node.getNext())));
Basically I am trying to add a second node to my list. I have tried 2 different approaches to adding a new node. But I get the same error message either way. I left the second approach commented out, so you can see what else I was trying.
It works fine with the initial node that I created, if I dont try to add any other nodes. Obviously I am not sure as to the correct method to use to add more nodes, so any and all help is appreciated.
Here is the code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LinkedLists extends JFrame
{
private JTextArea showList;
private JButton print, reverse;
DLLNode node = new DLLNode("One",null);
// error message HERE on this next line
node.setPrevious(node.setNext(new DLLNode("Two",node.getNext())));
// node.setNext(new DLLNode("Two",node.getNext()));
Â
public LinkedLists()
{
 Â
 setTitle("Lab 5 - Double Linked Lists");
Â
 showList = new JTextArea(4,1);
 Â
 print = new JButton("Print to Screen");
 print.addActionListener(new ActionListener(){
Â
 public void actionPerformed (ActionEvent e)
 {  Â
  printList(node);  Â
 }
 });
Â
 Â
 reverse = new JButton("Reverse List");
 reverse.addActionListener(new ActionListener(){
Â
 public void actionPerformed (ActionEvent e)
 {  Â
  //reverseList();  Â
  Â
 }
 });
Â
 Â
 Container cp = this.getContentPane();
 cp.setLayout(new ColumnLayout(1,10,10,ColumnLayout.WIDTH));
 cp.add(new JLabel("Double Linked List"));
 JPanel buttonPanel = new JPanel();
 buttonPanel.setLayout(new FlowLayout());
 buttonPanel.add(print);
 buttonPanel.add(reverse);
 cp.add(showList);
 cp.add(buttonPanel); Â
 pack();
 setSize(new Dimension(350, 200));
 setVisible(true);
}
 Â
  public void printList(DLLNode head)
  {
  DLLNode current = head;  Â
   Â
    while (current != null)
    {    Â
 showList.append(current.toString());       Â
      current = current.getNext();
      if (current == head) break;
    }    Â
  }
 Â
  public DLLNode reverseList(DLLNode head)
  {
  DLLNode temp = head;
 Â
  return temp;
  }
 Â
  public static void main(String[] args)
  {
         Â
    LinkedLists ll = new LinkedLists();
Â
 ll.setDefaultCloseOperation(
 JFrame.EXIT_ON_CLOSE);            Â
  }   Â
}
Here is the DLLNode class, this class was provided by the instructor for us to use:
public class DLLNode
{
private String data;
private DLLNode nextnode=null;
private DLLNode prevnode=null;
/** Default constructor
*/
public DLLNode() {
}
/** This constructor accepts a String and the
* node that is the end of the list and adds
* this new node after that and updates the
* references
*/
public DLLNode(String str, DLLNode node) {
data=str;
if(node != null) {
this.setPrevious(node);
node.setNext(this);
}
}
/** This method sets the next node instance data
*/
public void setNext(DLLNode node) {
nextnode=node;
}
/** This method sets the previous node instance data
*/
public void setPrevious(DLLNode node) {
prevnode=node;
}
/** This method returns the next node instance data
*/
public DLLNode getNext() {
return nextnode;
}
/** This method returns the previous node instance data
*/
public DLLNode getPrevious() {
return prevnode;
}
/** This method returns the String instance data
*/
public String getData() {
return data;
}
/** This method returns a String representation of the object
*/
public String toString() {
return "[DLLNode data = "+data+" nextnode = "+ nextnode+
" previous node = "+prevnode+" ]\n";
}
/** This method performs a equals comparison between this object
* and a supplied object returning true if they are equal
*/
public boolean equals(Object o) {
if(!(o instanceof DLLNode))
{
return false;
}
DLLNode nxt = (DLLNode) o;
if (getData() == nxt.getData() &&
getNext() == nxt.getNext() &&
getPrevious() == nxt.getPrevious())
{
return true;
}
return false;
}
}
This post has been edited by jayman9: 09 February 2006 - 10:45 PM

New Topic/Question
Reply




MultiQuote



|