私は現在、2つのサイコロを振る単純なGUIアプリケーションに取り組んでいます。現在、「ゲーム」と「ダイスロール」の2つのクラスを使用しています。割り当て基準を満たすには、プログラムを機能させるために複数のクラスを使用する必要があります。1つのクラスを使用するだけの方がはるかに簡単です...とにかく、switchステートメントを使用して「Graphics.drawImage()」メソッドを順番に実装する「DiceRoll」クラスで「roll()」メソッドを正常に呼び出しています。指定された「.png」画像を描画します。すべてが正常に見え、実行する前にエラーは発生しません。プログラムを実行すると、GUIフレームがポップアップし、作成したすべてのボタン/メニューが機能します。ただし、「ロールダイス」ボタンを押すと、複数の実行時エラーが発生し続けます。これは私がこれまでに持っているものです:
ゲームクラス
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Game extends JFrame implements ActionListener
{
JPanel mainPanel;
JPanel optionPanel;
JPanel dicePanel;
JButton rollDice;
JMenu options;
JMenuItem quit;
JMenuItem explanation;
JMenuBar menuBar;
JLabel diceLabel;
JLabel diceLabel2;
DiceRoll dr;
Graphics die1;
Graphics die2;
public Game()
{
setTitle("Rollin' Dice");
this.setPreferredSize(new Dimension(600,600));
mainPanel = new JPanel();
optionPanel = new JPanel();
dicePanel = new JPanel();
rollDice = new JButton("Roll Dice");
options = new JMenu("Options");
quit = new JMenuItem("Quit");
explanation = new JMenuItem("Explanation");
menuBar = new JMenuBar();
dr = new DiceRoll();
diceLabel = new JLabel();
diceLabel2 = new JLabel();
options.add(quit);
options.add(explanation);
menuBar.add(options);
optionPanel.add(menuBar);
optionPanel.setPreferredSize(new Dimension(600,100));
dicePanel.add(rollDice);
dicePanel.add(diceLabel);
dicePanel.add(diceLabel2);
mainPanel.setPreferredSize(new Dimension(600,600));
mainPanel.add(optionPanel);
mainPanel.add(dicePanel);
quit.addActionListener(this);
explanation.addActionListener(this);
rollDice.addActionListener(this);
this.getContentPane().add(mainPanel);
this.pack();
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()== quit)
System.exit(0);
if (e.getSource() == explanation)
{
JOptionPane.showMessageDialog(mainPanel,
"Win: Roll a sum that is an even number \nLose: Roll a sum that is an odd number" + dicePanel, "Rules", JOptionPane.INFORMATION_MESSAGE);
}
if (e.getSource() == rollDice)
{
dr.roll(die1);
dr.roll(die2);
diceLabel.updateUI();
dicePanel.updateUI();
}
}
public static void main (String []args)
{
Game dg = new Game();
}
}
DiceRollクラス
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.imageio.*;
import java.io.File;
import java.io.IOException;
import javax.swing.JComponent;
public class DiceRoll extends JComponent {
private BufferedImage die1;
private BufferedImage die2;
private BufferedImage die3;
private BufferedImage die4;
private BufferedImage die5;
private BufferedImage die6;
public DiceRoll()
{
try {
die1 = (ImageIO.read(new File("die1.png")));
die2 = ImageIO.read(new File("die2.png"));
die3 = ImageIO.read(new File("die3.png"));
die4 = ImageIO.read(new File("die4.png"));
die5 = ImageIO.read(new File("die5.png"));
die6 = ImageIO.read(new File("die6.png"));
} catch (IOException ex){
System.err.println("That is invalid");
}
}
public Graphics roll(Graphics g)
{
int dieResult = (int)(6 * Math.random());
switch(dieResult){
case 1:
g.drawImage(die1, 0, 0, null);
break;
case 2:
g.drawImage(die2, 0, 0, null);
break;
case 3:
g.drawImage(die3, 0, 0, null);
break;
case 4:
g.drawImage(die4, 0, 0, null);
break;
case 5:
g.drawImage(die5, 0, 0, null);
break;
case 6:
g.drawImage(die6, 0, 0, null);
break;
}
return g;
}
}
私が受け取っているエラー
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at DiceRoll.roll(DiceRoll.java:51)
at Game.actionPerformed(Game.java:89)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)