I am trying to code a Tetris game by myself. I made a class that handles the controls, in which the following happens:
- Asks if the player is okay with the default controls and lists them out
- If they click no, then another
JFrame
pops up asking which button they want to change. This is where I am creating theArrayList
of JButtons so I don't have to make each one separately when they all do basically the same thing. - When someone clicks on one of the controls, it comes up with another JFrame asking for the key they want to replace that control with.
Now here is the issue. When I run my code, after clicking no (that I want to change a control), the console throws a null-pointer exception. I am confused by this, is there anything I am missing? I am new to using JFrame, here is my method that handles all of this:
public void changeControls() {
JFrame controls = new JFrame();
JPanel panelControls = new JPanel();
JLabel controlLabel = new JLabel("Change which control?");
controls.add(panel, BorderLayout.CENTER);
controls.setPreferredSize(new Dimension(600, 600));
controls.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
controls.setTitle("Change Controls");
controls.setVisible(true);
controls.pack();
panelControls.setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
panelControls.setSize(600, 600);
panelControls.setLayout(new GridLayout(0, 1));
panelControls.add(controlLabel);
String[] names = {"Move Piece Right", "Move Piece Left", "Soft Drop", "Hard Drop", "Rotate Right", "Rotate Left"};
ArrayList<JButton> buttons = new ArrayList<JButton>();
for (int i = 0; i < 6; i++) {
buttons.add(new JButton(names[i]));
final Integer counter = Integer.valueOf(i);
final String type = names[i];
buttons.get(i).addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
JFrame editControl = new JFrame();
JPanel editControlPanel = new JPanel();
JLabel editLabel = new JLabel("Change controls for " + names[counter]);
JButton okay = new JButton("Click to Change to above text.");
JTextField text = new JTextField();
text.setColumns(1);
editControl.add(panel, BorderLayout.CENTER);
editControl.setPreferredSize(new Dimension(600, 600));
editControl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
editControl.setTitle("Change Controls");
editControl.pack();
editControlPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
editControlPanel.setSize(600, 600);
editControlPanel.setLayout(new GridLayout(0, 1));
editControlPanel.add(editLabel);
editControlPanel.add(text);
okay.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
editControl.dispose();
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
});
editControlPanel.add(okay);
KeyStroke ks = KeyStroke.getKeyStroke(text.getText());
switch(type) {
case "Move Piece Right":
moveRight = ks.getKeyCode();
case "Move Piece Left":
moveLeft = ks.getKeyCode();
case "Soft Drop":
softDrop = ks.getKeyCode();
case "Hard Drop":
hardDrop = ks.getKeyCode();
case "Rotate Right":
rotateRight = ks.getKeyCode();
case "Rotate Left":
rotateLeft = ks.getKeyCode();
}
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
});
panelControls.add(buttons.get(i));
}
controls.dispose();
startGame();
}
Thanks for all the help in advance. Also, the error is at this line:
controls.add(panel, BorderLayout.CENTER);