1
static JPanel fieldPanel = new JPanel();

static char cell[][][] = new char[2][12][12];
static JButton jCell[][][] = new JButton[2][12][12];
public void initField(){
    for (int y=1; y<11; y++){
        for (int field=0; field<2; field++ ){
            for (int x=1; x<11; x++){
                cell[field][x][y] = '.';
                jCell[field][x][y].setBounds((x * 20) + (field * 200), y * 20, 15, 15);
                fieldPanel.add(jCell[field][x][y]);
            }
        }
    }
}

Exception in thread "main" java.lang.NullPointerException in the longest string/

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • You haven't initialized the 1st and 2nd dimensions of `cell` and none of `jCell`. – Sotirios Delimanolis Jan 22 '14 at 15:51
  • Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). That code seems better suited to a `GridLayout`, e.g. as seen in [Making a robust, resizable Chess GUI](http://stackoverflow.com/q/21142686/418556). – Andrew Thompson Jan 22 '14 at 17:04

4 Answers4

1

You need to create JButton like below and then you can set the bounds.

jCell[field][x][y] = new JButton();
jCell[field][x][y].setBounds((x * 20) + (field * 200), y * 20, 15, 15);
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
  • They (preferred size and bounds) are both terrible suggestions. See See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) as well as the answer linked in my first comment above. – Andrew Thompson Jan 22 '14 at 17:06
0

jCell[field][x][y] is null just before you call jCell[field][x][y].setBounds(), hence the NullPointerException.

Bludzee
  • 2,733
  • 5
  • 38
  • 46
0

static JButton jCell[][][] = new JButton[2][12][12]; just create the 3D array of references to JButtons but :

  • no JButton is created
  • array is filled with null

Then when you call jCell[field][x][y].whatYouWant you try to dereference null.

Initialize your jCell array with buttons :

for (i=0; ...
  for (j=0; ...
    for (k=0; ...
      jCell[i][j][k] = new JButton(...);
Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
0
Type[] var = new Type[2];

will only allocate space in memory to hold 2 Type objects and initialize each entry to null. This means that:

var[0] == null

evaluates to true after that.

Thus

var[0].method();

throws a NullPointerException.

You need to initialize each entry of your array before you can deference them:

jCell[field][x][y] = new JButton(...);
jCell[field][x][y].setBounds(...);

or:

JButton button = new JButton(...);
button.setBounds(...);
jCell[field][x][y] = button;
desseim
  • 7,968
  • 2
  • 24
  • 26