0

So I'm trying to implement a guess and check game where there are letters of the English alphabet displayed then each time one is clicked it goes away if it wasn't part of the word specified. (I guess you can call it hangman but there's going to be more to it). Then once the game is lost setLetters is called again and should reset all the letters back onto the screen. (that's another problem i haven't solved in this and know belongs between the update and setLetters() )

letters = new JLabel[26];

for (int i = 0; i < 26; i++) {
    int ch = 'A' + i;
    letters[i] = new JLabel("" + (char) ch);
    panel.add(letters[i],c);
}
ctrl.setLetters(letters);

in the ctrl class there is

public void setLetters(JLabel[] letters2) {
    letterLabel = letters2;
    for (int i = 0; i < letters2.length; i++) {
        letterLabel[i].addMouseListener(this); 
/* i'm assuming this issue is here with adding tons of mouselisteners to the label each time it resets, but i'm not sure how to fix it */
    }
}

@Override
public void mousePressed(MouseEvent e) {

    for (int i = 0; i < 26; i++) {
        if (e.getSource() == letterLabel[i]) {
            if    (!game.letterAvailable((letterLabel[i].getText().charAt(0)))){
                letterLabel[i].setText(" ");
            }
            else {
                game.makeGuess((letterLabel[i].getText().charAt(0)));
            }
        }

    }

    update();
}

public void update() {
    if (letterLabel != null) {
        setLetters(letterLabel);
    }
    if (panel != null) { // redraw (not important for this error)
        panel.repaint();
    };
}

the error I get is exponentially that of:

java.awt.AWTEventMulticaster.mouseExited(Unknown Source) OR java.awt.AWTEventMulticaster.mouseExited(Unknown Source)

depending on how the letters are clicked (have yet to figure out why)

user2958542
  • 331
  • 1
  • 8
  • 18
  • 1
    Whenever you get an exception you want to post the stack trace and mention what line the exception is occurring. Also when asking a question here, you should actually _ask a question_ – Paul Samsotha Feb 22 '14 at 15:16
  • @peeskillet There are two many exceptions occurring, so I can't scroll up in the console (using eclipse) to see the line it's occurring at. – user2958542 Feb 22 '14 at 15:23
  • I believe eclipse console holds up to 1500 lines. If your stack trace is longer than that than your have a _serious_ problem. – Paul Samsotha Feb 22 '14 at 15:27
  • The whole point is, you're giving us too little information to go off of to try and help you out. And you haven't even asked a question, so what exactly are we supposed to answer? – Paul Samsotha Feb 22 '14 at 15:28
  • The only thing in my console is that error repeating. The question is: Why is this error occurring? I have given all code that runs with mouseListener and after a few clicks on different labels, I get what's occurring to me. I will edit the question to show exactly what's in the console right now. – user2958542 Feb 22 '14 at 15:35
  • Post [a Minimal, Complete, Tested and Readable example](http://stackoverflow.com/help/mcve) that we can test out. – Paul Samsotha Feb 22 '14 at 15:38

1 Answers1

0

As stated in this answer from HoverCraftFullOfEeels

Don't add the same listener to a component inside of its listener code.

In you're mousePressed you're calling update() which calls setLetter, which adds a listener to the component.

Looks like you will have to do some refactoring. Can't really help you any further without full understanding what you're trying to accomplish.

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720