2

私は単純なゲーム アプリケーションで、700 行から 177 行まですべてを合理化し、繰り返しをなくしていました。アプリケーションを実行しようとすると、Exception in thread "AWT-EventQueue-0" java.lang.NullPointerExceptionスローされます。私はそれが何を意味するかを(これを読んで)知ってactionListenerます

私は配列を扱って出くわしたすべてのチュートリアルに従いましたが、JButtons少し問題が発生しているに違いありません。private JButton[][] blocks = new JButton[rows][cols]は正しく宣言されているので、そうではなくblocks[i][j].addActionListener(this)、block1、block2 ... などをblock[i][j].

これが私に投げられたものです:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at blox.Blox.game(Blox.java:105)
    at blox.Blox.access$000(Blox.java:25)
    at blox.Blox$1.run(Blox.java:173)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:697)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

そして、ここに私のコードがあります:

public class Blox implements ActionListener {

  private final String gameVersion = "Blox - v1.0.2";
  private final int rows = 7;
  private final int cols = 7;

  // Board Inits
  private ImageIcon purpleBlock = new ImageIcon();
  private ImageIcon redBlock = new ImageIcon();
  private ImageIcon blueBlock = new ImageIcon();
  private ImageIcon greenBlock = new ImageIcon();
  private ImageIcon closeImage = new ImageIcon();
  private JButton[][] block = new JButton[rows][cols];
  private JPanel boardBg = new JPanel();

  // Error console init
  private JLabel errorMessage = new JLabel();
  private JPanel errorBg = new JPanel();
  private JButton closeError = new JButton();

  // Score/UI Inits
  private JButton resetBoard = new JButton("Reset Board");

  /**
   *  700 x 480 Frame Size
   *  Sets Up and displays initial scene.
   *
   */
  private void game() {

    // Initialize mainFrame
    JFrame mainFrame = new JFrame(gameVersion);
    Dimension minSize = new Dimension(700,480);
    mainFrame.setMinimumSize(minSize);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.setLayout(null);
    mainFrame.setResizable(false);
    try {
      mainFrame.setContentPane(new JLabel(new ImageIcon(
          ImageIO.read(new File("bggrad.jpg")))));
    } catch (IOException e) {
      e.printStackTrace();
    }

    // Setup block Images
    try {
      purpleBlock.setImage(ImageIO.read(new File("purpleBlock.png")));
      redBlock.setImage(ImageIO.read(new File("redBlock.png")));
      blueBlock.setImage(ImageIO.read(new File("blueBlock.png")));
      greenBlock.setImage(ImageIO.read(new File("greenBlock.png")));
      closeImage.setImage(ImageIO.read(new File("close.jpg")));
    } catch (IOException e) {
      e.printStackTrace();
    }

    // Sets up error message panel
    // Only displayed if error occurs on ActionListener
    errorMessage.setText("");
    errorMessage.setForeground(Color.black);
    Font newLabelFont = new Font(errorMessage.getFont().getName(),
        Font.BOLD,errorMessage.getFont().getSize());
    errorMessage.setFont(newLabelFont);
    closeError.setIcon(closeImage);
    closeError.setBounds(0,0,24,24);
    errorBg.setBounds(0,430,700,30);
    errorBg.setBackground(Color.lightGray);
    errorBg.add(closeError);
    errorBg.add(errorMessage);
    errorBg.setVisible(false);

    // Sets up Score/UI Portions of Screen
    resetBoard.setBounds(60,363,175,25);
    resetBoard.setVisible(true);
    resetBoard.addActionListener(this);

    // Set Up Board
    boardBg.setLayout(new GridLayout(rows,cols));
    boardBg.setBounds(300,50,350,350);
    for(int i=0; i<rows; i++) {
      for(int j=0; i<cols; j++) {
        block[i][j].setIcon(randomizer());
        block[i][j].addActionListener(this);
        boardBg.add(block[i][j]);
      }
    }

    mainFrame.getContentPane().add(boardBg);
    mainFrame.getContentPane().add(errorBg);
    mainFrame.getContentPane().add(resetBoard);

    //mainFrame.pack();
    mainFrame.setVisible(true);
  }


  /**
   *  Checks onClick event for which JButton was clicked
   *  and changes JButton.setIcon() accordingly.
   *
   *  @param  none
   *  @return none
   *  @see    java.awt.event.*
   *  @see    JButton
   *  @see    setIcon()
   *  @see    getIcon()
   *
   */
  public void actionPerformed(ActionEvent e) {

    if(e.getSource()==resetBoard) {
      System.out.println("Resetting");
      gameReset();
    }

    if(e.getSource()==errorBg || e.getSource()==errorMessage) {
      errorBg.setVisible(false);
    }
  }

  public void gameReset() {

    for(int i=0; i<rows; i++) {
      for(int j=0; i<cols; j++) {
        block[i][j].setIcon(randomizer());
      }
    }
  }

  public ImageIcon randomizer() {

    Random r = new Random();
    int rNum = r.nextInt(4);

    if(rNum==0) {
      return purpleBlock;
    } else if(rNum==1) {
      return redBlock;
    } else if(rNum==2) {
      return greenBlock;
    } else {
      return blueBlock;
    }
  }

  public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        new Blox().game();
      }
    });
  }
}
4

1 に答える 1

4

問題はおそらくあなたのループから来ています:

    for(int i=0; i<rows; i++) {
        for(int j=0; i<cols; j++) {
            block[i][j].setIcon(randomizer());
            block[i][j].addActionListener(this);
            boardBg.add(block[i][j]);
        }
    }

block[i][j]初期化されていません。最初に次のように初期化します。

    for(int i=0; i<rows; i++) {
        for(int j=0; i<cols; j++) {.
            block[i][j] = new JButton();
            block[i][j].setIcon(randomizer());
            block[i][j].addActionListener(this);
            boardBg.add(block[i][j]);
        }
    }
于 2013-02-22T17:05:05.680 に答える