Search code examples
javalistpointerslinked-listtraversal

Program losing Java linked list pointers


I'm having a problem I've spent hours trying to fix on my program. My linked list works, I can add objects to it and those objects all have previous pointers while creating, but when I try to traverse the list by pressing a button, the pointers don't seem to work. It will go to the previous node but then stop working completely with no exceptions. I really cannot understand what is wrong, but I bet it is something really simple like a line of code being in the wrong place or something.

Engine class:

public TeamList tl = new TeamList();
public Team[] t = new Team[0];
private int x = 0;

public void createTeams() {
    x++;
    System.out.println("Enter team name:");
    String teamName;
    teamName = GUI.EditTeams.jTextField1.getText();
    t = Arrays.copyOf(t, (t.length + 1)); 
    t[x - 1] = new Team(x, teamName); /
    if (t[x - 1].getTeamNumber() == 1) { 
        tl.addTeam(t[x - 1]); 
        tl.current = t[x - 1];
    } else {
        tl.addTeam(t[x - 1]); 
        t[x - 1].setPrev(t[x - 2]);
        tl.current = t[x - 1];
    }
    printAllTeams(t);
    EditTeams.jTextField1.setText("");
    EditTeams.jTextField3.setText(t[x - 1].getTeamName());
    System.out.println("Team added!");
}

GUI class action performed on previous team button press:

    try {
        sm.prevTeam();
    } catch (InterruptedException ex) {
        Logger.getLogger(EditTeams.class.getName()).log(Level.SEVERE, null, ex);
    }


prevTeam():

public void prevTeam() throws InterruptedException {
    if (t.length == 0 || tl.current.getPrev() == null) {
        EditTeams.jTextField3.setText("Error: No more teams available");
        Thread.sleep(2000);
        EditTeams.jTextField3.setText("");
    } else {
        tl.traverse('p', t[x - 1]);
        EditTeams.jTextField3.setText(tl.current.getTeamName());
    }
}

Here is the linked list:

public class TeamList {

public Team head;
public Team current;

public void addTeam(Team newTeam) // Method to add an item
{
    if (head == null) // If the head is null then head becomes the new item
    {
        head = newTeam;
    } else {  // Else the current is the head
        current = head;
        while (current.getNext() != null) // While the next node is not null, set the current node as the next node
        {
            current = current.getNext();
            current.setNext(newTeam); // Once at the end, the current node becomes the new item
        }
    }
}

public void traverse(char nextOrPrev, Team team)
{
    if (nextOrPrev == 'n') {
        current = team.getNext();
    } else if (nextOrPrev == 'p') {
        current = team.getPrev();
    }
    //team.position = current;
    //current. = p;
}

}

Sorry if I've done anything wrong, I am not a StackOverflow pro and I am not a programming pro (please don't laugh at what I've coded). I have seen people saying stuff should be marked as homework help. This is homework help. Thanks very much in advance.


Solution

  • I shall answer my own question for the purpose of anyone who looks at this. The problem was in the calling of the traverse method in the prevTeam method.

    The following line:

    tl.traverse('p', t[x - 1]);
    

    Should be:

    tl.traverse('p',tl.current);
    

    Basically, I was telling to traverse from the most recently added team every time, so if there were two teams, kept going to team 1 over and over again because team 1 was the previous team of t[x-1]. I was using the latest array object rather than the linked list.