Acetamide Acetamide - 15 days ago 9
Java Question

The data in linked list won't show on the Text Area in GUI. Java. Why?

I am currently doing a book inventory system in linked list using Java GUI. I have to add book information into a Node in linked list and display it by implementing the iterator.

I have done with my code and it shows no error. However, when I run the GUI and successfully add a book into the linked list then press the display button. It did not show the information I just added to the text area.

Did anywhere of my code have problem?

This is my Node class:

public class Node
{
Data data;
Node next;

public Node()
{
next = null;
}

Node(Data data, Node next)
{
this.data = data;
this.next = next;
}

public Object getData()
{
return data;
}

public Node getNext()
{
return next;
}

public void setNext(Node next)
{
this.next=next;
}
}


And this is my LinkedList class with the insertion and display method:

public class LinkedList
{
Node node = new Node();
static Data data;
static Node head;

public LinkedList()
{
head=null;
}

public Node getHead()
{
return head;
}

public static void addNode(Data data)
{
Node newNode = new Node(data, head);
Node previous = null;
Node current = head;

while(current != null && data.name.compareTo(current.data.name) >= 0){
previous = current;
current = current.next;
}

if(previous == null){
head = newNode;
}else{
previous.next = newNode;
}
newNode.next = null;
JOptionPane.showMessageDialog(null,"Book Information has been added to the inventory.");
}
}

public static String displayNode()
{
DisplayIterator i;
Node current = head;
String output = "";
while(DisplayIterator.hasNext())
{
output+= DisplayIterator.next();
current=current.next;
}
return output+"NULL";
}


This is my data class which I used to store all the information into one Node:

public class Data {
String name;
String author;
int isbn;
int number;
String genre;
Node head;

public Data(String name, String author, int isbn, int number, String genre)
{
this.name = name;
this.author = author;
this.isbn = isbn;
this.number = number;
this.genre = genre;
}

public String toString(String name, String author, int isbn, int number, String genre)
{
return("Book Name: "+name+"\nAuthor: "+author+"\nISBN Number: "+isbn+"\nNumber of Copies: "+number+"\nGenre: "+genre+"\n");
}
}


And lastly this is my Iterator class:

public class DisplayIterator
{
Data data;
static Node current;

DisplayIterator(Data data)
{
this.data = data;
current = data.head;
}

public static boolean hasNext()
{
if(current != null){
return true;
}
return false;
}

public static Object next()
{
if(hasNext()){
current = current.getNext();
return current.getData().toString();
}
return null;
}

public void remove()
{
throw new UnsupportedOperationException("It is read-only.");
}
}


I think the problem is on the DisplayIterator class but I can't see where.
Can anyone help me? Thank you.

Answer

Your data.head is always null

DisplayIterator(Data data)
{       
    this.data = data;
    current = data.head;  // this will always make current as null.  
}

In your following class , the constructor does not initialize your head node.

public class Data {
   String name;
   String author;
   int isbn;
   int number;
   String genre;
   Node head; // not set any where?

Also your addNode is not correct.

public static void addNode(Data data)
{
    Node newNode = new Node(data, head);
    Node previous = null;
    Node current = head; //this will be always NULL on first addNode call

    while(current != null && data.name.compareTo(current.data.name) >= 0){
        previous = current;
        current = current.next;
    }

    if(previous == null){
        head = newNode;
    }else{
        previous.next = newNode;
    }
        newNode.next = current; // how can be newNode.next is current?
        JOptionPane.showMessageDialog(null,"Book Information has been added to the inventory.");
}
Comments