0

I have an ArrayList of 9 JButtons, I'm looping on it ti identify every time which buttons are not played yet, but it returns the exception as Exception in thread "Thread-1" java.lang.NullPointerException

JButton buttons[] = { btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9 };
ArrayList<JButton> vide = new ArrayList<JButton>();

t = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            count++;
            for (int i = 0; i < buttons.length; i++) {
                if (!buttons[i].getText().equals("X")
                        && !buttons[i].getText().equals("O"))
                    vide.add(buttons[i]);
            }
            count++;
            Random btn = new Random();
            vide.get(btn.nextInt(vide.size())).setText("O");

            Thread.currentThread().interrupt();
        }
    });

the problem is on the loop, I'm implementing tic tac toe game of one user with a machine, this loop is to identify which buttons could be played with the machine by thread.

thar45
  • 3,518
  • 4
  • 31
  • 48
devhicham
  • 557
  • 2
  • 6
  • 14
  • 2
    Please provide a description of the exception you see. – andrel Mar 14 '17 at 20:25
  • Also, please provide the code that creates the buttons – marisbest2 Mar 14 '17 at 20:27
  • 2
    side question : why the call to `currentThread().interrupt()` at the last line of run !!!!? – niceman Mar 14 '17 at 20:29
  • 1
    What is the purpose of creating a thread to run this code? Your code is definitely not thread safe if anything other than this thread interacts with your data structures. – Daniel Pryden Mar 14 '17 at 20:30
  • 1
    as for the exception I'm almost sure that one of your `btn1/2/3/4/...` is `null` when the Thread you start try to access them `buttons[i].getText()`, see this question : http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – niceman Mar 14 '17 at 20:31
  • why count++, 2 times ? – Abhishek Aryan Mar 14 '17 at 20:33
  • can you please add the complete code statements of btn1 to 9 def's too – thar45 Mar 14 '17 at 20:56

0 Answers0