そこで、ボタンで制御されて迷路を走るボールの GUI を作成しようとしています。しかし、まず、いくつかの方向ボタンを備えたフレームを作成したいと思います。これをEclipseで実行しようとすると、あまり具体的ではないエラーが発生します。私のコードは以下です。エラーは、コードの「buttonTL、buttonTR、buttonBL、および buttonBR」セクションを指しています。ご協力いただきありがとうございます。
エラー:
Exception in thread "main" java.lang.NullPointerException at mazeassignment.One.createGUI(One.java:55) at mazeassignment.One.main(One.java:23)
import java.awt.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class One extends JFrame implements ActionListener//, ChangeListener
{
private JButton buttonRight, buttonLeft, buttonUp, buttonDown, buttonPlay, buttonExit, buttonReset, buttonGrid, buttonTL, buttonTR, buttonBL, buttonBR, buttonM, buttonOption1, buttonOption3, buttonCompass;
private JPanel panelCentre, panelRight, panelBottom, buttonPanel, compassPanel, optionsPanel, debugPanel;
private JSlider sliderSpeed;
private JLabel appSpeed;
private JButton [] JBArray = new JButton[630];
private int bLocation = 290;
public int ballLocation = 287;
private Icon bImage, iconBall, tile, iconWall, iconCompassNorth;
public static void main (String[] args)
{
One frame = new One();
frame.setSize(875, 600);
frame.createGUI();
frame.setVisible(true);
}
private void createGUI()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new BorderLayout() );
panelCentre = new JPanel();
panelCentre.setPreferredSize(new Dimension(625, 450));
panelCentre.setBackground(Color.BLUE);
window.add(panelCentre);
panelCentre.setLayout(new GridLayout(21, 30));
panelRight = new JPanel();
panelRight.setPreferredSize(new Dimension(200, 450));
panelRight.setBackground(Color.GRAY);
window.add(panelRight, BorderLayout.EAST);
panelBottom = new JPanel();
panelBottom.setPreferredSize(new Dimension(875, 100));
panelBottom.setBackground(Color.GREEN);
window.add(panelBottom, BorderLayout.SOUTH);
debugPanel = new JPanel();
debugPanel.setPreferredSize(new Dimension(200, 100));
debugPanel.setBackground(Color.WHITE);
panelRight.add(debugPanel);
buttonTL = new JButton("");
buttonPanel.add(buttonTL);
buttonTL.setEnabled(false);
buttonUp = new JButton("^");
buttonPanel.add(buttonUp);
buttonUp.addActionListener(this);
buttonTR = new JButton("");
buttonPanel.add(buttonTR);
buttonTR.setEnabled(false);
buttonLeft = new JButton("<");
buttonPanel.add(buttonLeft);
buttonLeft.addActionListener(this);
buttonM = new JButton("");
buttonPanel.add(buttonM);
buttonM.setEnabled(false);
buttonRight = new JButton(">");
buttonPanel.add(buttonRight);
buttonRight.addActionListener(this);
buttonBL = new JButton("");
buttonPanel.add(buttonBL);
buttonBL.setEnabled(false);
buttonDown = new JButton("v");
buttonPanel.add(buttonDown);
buttonDown.addActionListener(this);
for(int i=0; i<630; i++)
{
JBArray[i] = new JButton();
buttonGrid = new JButton(""+i);
panelCentre.add(JBArray[i]);
JBArray[i].setBorderPainted(false);
}
}
public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();
JBArray[bLocation].setIcon(iconBall);
JBArray[bLocation].setIcon(tile);
if (source == buttonRight)
{
JBArray[bLocation+1].setIcon(iconBall);
JBArray[bLocation-1].setIcon(tile);
bLocation=bLocation+1;
}
if (source == buttonLeft)
{
JBArray[bLocation-1].setIcon(iconBall);
JBArray[bLocation+1].setIcon(tile);
bLocation=bLocation-1;
}
if (source == buttonUp)
{
JBArray[bLocation-30].setIcon(iconBall);
JBArray[bLocation+30].setIcon(tile);
bLocation=bLocation-30;
}
if (source == buttonDown)
{
JBArray[bLocation+30].setIcon(iconBall);
JBArray[bLocation-30].setIcon(tile);
bLocation=bLocation+30;
}
}
}