So I'm trying to create a GUI for a ball that is controlled by buttons to run through a maze. But just to begin with I want to create a frame with a few directional buttons and whenever I try to run this in eclipse it comes up with an error that is not very specific at all. My code is below. The error points to the "buttonTL, buttonTR, buttonBL and buttonBR" sections of the code. Thank you for any help you can provide.
The error is:
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;
}
}
}
You forgot to initialize buttonPanel. Add this before you start to use it:
buttonPanel= new JPanel();
Then it works fine.