JToggleButton blank screen

M

mrthis

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);
}
}
 
T

Thomas Fritsch

mrthis said:
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();
// You got a NullPointerException here!
// The reason is: c still is null.
cell[x][y].setText("yes!");
cp.add(cell[x][y]);
}
}
pack();
} 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).
}
}

public static void main(String[] args) {
Minesweeper sweeper = new Minesweeper();
sweeper.setVisible(true);
}
}
 
V

Vova Reznik

mrthis said:
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[][]; cell not initalized

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 not initalized
NullPointerException
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);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top