Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2005

Tip: Looking for answers? Try searching our database.

JToggleButton blank screen

Thread view: 
mrthis - 17 Oct 2005 15:46 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.
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.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.