I am writing a minesweeper game for a class and have just gotten
started. I cannot figure out why this program gives me a blank screen.
I think it has something to do with how I have set up my array. Any
guidance gratefully accepted. /ml
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* <p>Title: Minesweeper </p>
* <p>Minesweeper game </p>
* <p>author Matthew Lasar</p>
* @version 1.0
*/
public class Minesweeper extends JFrame {
JToggleButton cell[][];
public Minesweeper() {
Container cp = getContentPane();
cp.setLayout(new GridLayout(16, 16));
try {
for (int x = 0; x < 16; x++) {
for (int y = 0; y < 16; y++) {
cell[x][y] = new JToggleButton();
cell[x][y].setText("yes!");
cp.add(cell[x][y]);
}
}
pack();
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) {
Minesweeper sweeper = new Minesweeper();
sweeper.setVisible(true);
}
}
Thomas Fritsch - 17 Oct 2005 15:59 GMT
> I am writing a minesweeper game for a class and have just gotten
> started. I cannot figure out why this program gives me a blank screen.
[quoted text clipped - 24 lines]
> for (int y = 0; y < 16; y++) {
> cell[x][y] = new JToggleButton();
// You got a NullPointerException here!
// The reason is: c still is null.
> cell[x][y].setText("yes!");
> cp.add(cell[x][y]);
[quoted text clipped - 3 lines]
> } catch (Exception e) {
> System.out.println(e);
// You should better use:
e.printStackTrace();
// Then you would have seen an exception stack trace,
// saying that it crashed in line 23 (see above).
> }
> }
[quoted text clipped - 5 lines]
> }
>

Signature
"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
Vova Reznik - 17 Oct 2005 17:18 GMT
> I am writing a minesweeper game for a class and have just gotten
> started. I cannot figure out why this program gives me a blank screen.
[quoted text clipped - 15 lines]
>
> JToggleButton cell[][];
cell not initalized
> public Minesweeper() {
> Container cp = getContentPane();
[quoted text clipped - 4 lines]
> for (int y = 0; y < 16; y++) {
> cell[x][y] = new JToggleButton();
cell not initalized
NullPointerException
> cell[x][y].setText("yes!");
> cp.add(cell[x][y]);
[quoted text clipped - 11 lines]
> }
> }
Roedy Green - 17 Oct 2005 22:45 GMT
> for (int y = 0; y < 16; y++) {
> cell[x][y] = new JToggleButton();
> cell[x][y].setText("yes!");
> cp.add(cell[x][y]);
you start filling your array without instantiating it .
See http://mindprod.com/jgloss/gotchas.html#MATRIX

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.