0

I write tic tac toe and i have small problem.

When checking the fields, console write error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at pl.knugi.Main.sprawdzKombinacje(Main.java:164)
at pl.knugi.Main.actionPerformed(Main.java:179)

This code:

    public boolean sprawdzKombinacje(String p){
    String[] spr = new String[3];
    for(JButton[] c : check){
        int a = 0;
        for(JButton b : c){
            spr[a]=b.getText();
            a++;
        }
        if(spr[0].equals(p)&&spr[1].equals(p)&&spr[2].equals(p)){
            return true;
        }
    }
    return false;
}

Variables:

JButton[] check1 = {button1, button2, button3};
JButton[] check2 = {button4, button5, button6};
JButton[] check3 = {button7, button8, button9};
JButton[] check4 = {button1, button4, button7};
JButton[] check5 = {button2, button5, button8};
JButton[] check6 = {button3, button6, button9};
JButton[] check7 = {button1, button5, button9};
JButton[] check8 = {button3, button5, button7};
JButton[][] check = {check1, check2, check3, check4, check5, check6, check7, check8};

What am i doing wrong?

Problem is b.getText(), because this is AbstractButton. Then how I getted text from my button ?

Patryk D
  • 1
  • 2
  • Also read [How do I compare strings in Java?](//stackoverflow.com/q/513832) – Tom May 03 '17 at 18:43
  • When you use the `==` operator on strings, it doesn't compare the words. Use `String.equals("your string here")` instead – Bennett Yeo May 03 '17 at 18:44
  • Thanks, but i find problem. My JButton b is AbstarctButton and i don't know how getText from my button from aplication. Line164 is spr[a]=b.getText(); – Patryk D May 03 '17 at 18:52

0 Answers0