Trying to build Sudoku board in Java Swing. NullPointerException error? -
i working on building gui java program generate , solve sudoku puzzles. way doing have 1 main jpanel, 3x3 jpanels inside it, have 3x3 jlabels in them.
here have far:
package sudoku; import java.awt.gridlayout; import javax.swing.*; import javax.swing.border.border; public class gui { gui gui; jpanel board; jpanel subboard[][]; gridlayout layout; jlabel square[][]; border big; border small; public void load() { gui = new gui(); gui.setup(); gui.buildboard(); } private void setup() { layout = new gridlayout(3, 3); board = new jpanel(layout); subboard = new jpanel[3][3]; square = new jlabel[9][9]; } private void buildboard() { // set board board.setvisible(true); int mod = 0; (int = 0; < 3; i++) { (int j = 0; j < 3; j++) { // add subboards board board.add(subboard[i][j]); subboard[i][j].setlayout(layout); // add textfields each subboard subboard[i][j].add(square[i + mod][j + mod]); } mod += 3; } } }
i getting nullpointerexception error on line:
board.add(subboard[i][j]);
can explain why happening? if see other problems, or better way doing please let me know! thanks.
in setup()
method @ line
subboard = new jpanel[3][3];
you've created array have not filled jpanel's yet.
so, default array contains null
every index position , hence when try add them board
(which unlike subboard
initialized new jpanel(layout)
) throws npe.
Comments
Post a Comment