java - Changing text of JLabel in ActionEvent -


ok, i'm creating tic tac toe gui. having working frame of 3x3 buttons change either x or o. issue implementing jlabel on top of buttons. label keeps track of who's turn , change either "o's turn" or "x's turn" can't seem jlabel update (only spouts errors). here's portion of code showing how trying implement this. program runs fine add (turn.settext("o's turn");) errors.

any feedback why may not working appreciated.

import java.awt.borderlayout; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener;  import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel;  public class tictactoe extends jframe {      xobutton[] xob = new xobutton[9];     private jlabel turn;      public static int state;      public tictactoe() {         super("tictactoe");          //add xobuttons panel         jpanel center = new jpanel();         state = 0;          jlabel turn = new jlabel("x's turn");          center.setlayout(new gridlayout(3, 3, 2, 2));         //action listener         buttonlistener bl = new buttonlistener();         (int = 0; < 9; i++) {             xob[i] = new xobutton();             xob[i].addactionlistener(bl);             center.add(xob[i]);         }          //add panel frame         setlayout(new borderlayout());         add(center, borderlayout.center);         add(turn, borderlayout.north);      }        //inner action listener class     private class buttonlistener implements actionlistener {          @override         public void actionperformed(actionevent ae) {             (int = 0; < 9; i++) {                 if(ae.getsource() == xob[i] && xob[i].getstate() == xobutton.none && state == 0) {                     xob[i].setstate(xobutton.x);                     state = 1;                     //turn.settext("o's turn");                  } else if(ae.getsource() == xob[i] && xob[i].getstate() == xobutton.none && state == 1) {                     xob[i].setstate(xobutton.o);                     state = 0;                 }             }         }     } } 

guess: error nullpointerexception because turn null. solution: don't shadow variable. in other words you're re-declaring turn variable in constructor leaving field null.

e.g.,

public class tictactoe extends jframe{ xobutton[] xob = new xobutton[9]; private jlabel turn;  public static int state;  public tictactoe() {     super("tictactoe");      //add xobuttons panel     jpanel center = new jpanel();     state = 0;      jlabel turn = new jlabel("x's turn");  // **** you're re-declaring turn here! 

better:

public class tictactoe extends jframe{ xobutton[] xob = new xobutton[9]; private jlabel turn;  public static int state;  public tictactoe() {     super("tictactoe");      //add xobuttons panel     jpanel center = new jpanel();     state = 0;      turn = new jlabel("x's turn");  // **** note difference? 

in future, if have similar problems, please post entire error message , indicate line throws exception immensely!