Reputation: 181
I'm new to coding and I'm trying to make a text-based, decision-based (hence the 1A variables and 1B variables) game. When I click the JButton west to evoke the action listener, the program cancels out and gives me this NullPointerException error.
I've tried using the Eclipse debugger, but when I do, a window pops up saying "JDI Thread evaluations has encountered a problem". Any thoughts on how I could fix this?
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
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.JPanel;
import javax.swing.JTextArea;
public class The_Room {
JFrame window;
Container container;
JPanel titleNamePanel;
JPanel mainPanel;
JPanel textPanel1A;
JPanel go_northButtonPanel;
JPanel westButtonPanel;
JPanel textPanel2A;
JPanel lift_paintingButtonPanel;
JPanel go_west_insteadButtonPanel;
JPanel lift_mattressButtonPanel;
JPanel go_north_insteadButtonPanel;
JButton startButton;
JButton go_north;
JButton west;
JButton lift_painting;
JButton go_west_instead;
JButton lift_mattress;
JButton go_north_instead;
JTextArea TextArea1A; //A JLabel that allows multiple lines of text
JTextArea TextArea2A;
JTextArea TextArea2B;
JLabel titleNameLabel;
TitleScreenHandler handler = new TitleScreenHandler();
Screen1AHandler handler1A = new Screen1AHandler();
Screen1BHandler handler1B = new Screen1BHandler();
public static void main(String[] args) {
new The_Room();
}
public The_Room() {
window = new JFrame();
window.setSize(800, 600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setBackground(Color.MAGENTA); //you can't change color w/o .getContentPane()
window.setLayout(null); //set it to null now (the default), so that you can customize the layout later
window.setVisible(true);
window.setTitle("THE ROOM");
container = window.getContentPane();
titleNamePanel = new JPanel();
titleNamePanel.setBounds(10, 100, 800, 150);
titleNamePanel.setBackground(Color.MAGENTA);
titleNameLabel = new JLabel("THE ROOM");
container.add(titleNamePanel);
titleNamePanel.add(titleNameLabel);
titleNameLabel.setFont(new java.awt.Font("Arial", Font.BOLD, 120));
titleNameLabel.setOpaque(true);
titleNameLabel.setBackground(Color.MAGENTA);
titleNameLabel.setForeground(Color.WHITE);
startButton = new JButton();
startButton.setText("START");
startButton.setFont(new java.awt.Font("Arial", Font.BOLD, 50));
startButton.setBounds(300, 300, 200, 100);
startButton.addActionListener(handler);
container.add(startButton);
}
public void createGameScreen() {
titleNamePanel.setVisible(false);
startButton.setVisible(false);
go_northButtonPanel = new JPanel();
go_northButtonPanel.setBounds(300,400,200,100);
go_northButtonPanel.setBackground(Color.MAGENTA);
go_north = new JButton();
go_north.setBackground(Color.BLACK);
go_north.setForeground(Color.BLACK);
go_north.setFont(new java.awt.Font("Arial", Font.BOLD, 25));
go_north.setBounds(300, 300, 200, 100);
go_north.addActionListener(handler1A);
go_northButtonPanel.add(go_north);
go_north.setText("GO NORTH");
container.add(go_northButtonPanel);
westButtonPanel = new JPanel();
westButtonPanel.setBounds(300,500,200,200);
westButtonPanel.setBackground(Color.MAGENTA);
container.add(westButtonPanel);
west = new JButton();
west.setBackground(Color.BLACK);
west.setForeground(Color.BLACK);
west.setFont(new java.awt.Font("Arial", Font.BOLD, 15));
west.setBounds(300, 500, 200, 200);
west.addActionListener(handler1B);
westButtonPanel.add(west);
west.setText("GO WEST");
textPanel1A = new JPanel();
textPanel1A.setBounds(0,100,800,850);
textPanel1A.setBackground(Color.MAGENTA);
container.add(textPanel1A);
TextArea1A = new JTextArea("You wake up in a small, cell-like room. Where are you? Who are you? You don't remember anything, but all you know is that you're trapped inside this room and you need to ESCAPE.");
TextArea1A.setBounds(0,100,800,850);
TextArea1A.setBackground(Color.MAGENTA);
TextArea1A.setForeground(Color.BLACK);
TextArea1A.setFont(new java.awt.Font("Arial", Font.PLAIN, 20));
TextArea1A.setLineWrap(true);
TextArea1A.setWrapStyleWord(true);
textPanel1A.add(TextArea1A);
}
public void createGameScreen2A() {
go_northButtonPanel.setVisible(false);
westButtonPanel.setVisible(false);
west.setVisible(false);
textPanel1A.setVisible(false);
TextArea1A.setVisible(false);
lift_paintingButtonPanel = new JPanel();
lift_paintingButtonPanel.setBounds(300,400,200,100);
lift_paintingButtonPanel.setBackground(Color.MAGENTA);
lift_painting = new JButton();
lift_painting.setBackground(Color.BLACK);
lift_painting.setForeground(Color.BLACK);
lift_painting.setFont(new java.awt.Font("Arial", Font.BOLD, 25));
lift_painting.setBounds(300, 300, 200, 100);
//lift_painting.addActionListener(handler1A);
lift_paintingButtonPanel.add(lift_painting);
lift_painting.setText("LIFT PAINTING");
container.add(lift_paintingButtonPanel);
go_west_insteadButtonPanel = new JPanel();
go_west_insteadButtonPanel.setBounds(300,500,200,200);
go_west_insteadButtonPanel.setBackground(Color.MAGENTA);
go_west_instead = new JButton();
go_west_instead.setBackground(Color.BLACK);
go_west_instead.setForeground(Color.BLACK);
go_west_instead.setFont(new java.awt.Font("Arial", Font.BOLD, 25));
go_west_instead.setBounds(300, 500, 200, 100);
go_west_insteadButtonPanel.add(go_west_instead);
go_west_instead.setText("GO WEST");
container.add(go_west_insteadButtonPanel);
textPanel2A = new JPanel();
textPanel2A.setBounds(0,100,800,850);
textPanel2A.setBackground(Color.MAGENTA);
container.add(textPanel2A);
TextArea2A = new JTextArea("You wake up in a small, cell-like room. Where are you? Who are you? You don't remember anything, but all you know is that you're trapped inside this room and you need to ESCAPE.");
TextArea2A.setBounds(0,100,800,850);
TextArea2A.setBackground(Color.MAGENTA);
TextArea2A.setForeground(Color.BLACK);
TextArea2A.setFont(new java.awt.Font("Arial", Font.PLAIN, 20));
TextArea2A.setLineWrap(true);
TextArea2A.setWrapStyleWord(true);
textPanel2A.add(TextArea2A);
}
public void createGameScreen2B() {
lift_paintingButtonPanel.setVisible(false); //LINE 190, where the code stops working
go_west_insteadButtonPanel.setVisible(false);
go_west_instead.setVisible(false);
textPanel2A.setVisible(false);
TextArea2A.setVisible(false);
lift_mattressButtonPanel = new JPanel();
lift_mattressButtonPanel.setBounds(300,400,200,100);
lift_mattressButtonPanel.setBackground(Color.MAGENTA);
lift_mattress = new JButton();
lift_mattress.setBackground(Color.BLACK);
lift_mattress.setForeground(Color.BLACK);
lift_mattress.setFont(new java.awt.Font("Arial", Font.BOLD, 25));
lift_mattress.setBounds(300, 300, 200, 100);
//lift_mattress.addActionListener(handler2B);
lift_mattressButtonPanel.add(lift_mattress);
lift_mattress.setText("LIFT PAINTING");
container.add(lift_mattressButtonPanel);
}
public class TitleScreenHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
createGameScreen();
}
}
public class Screen1AHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
createGameScreen2A();
}
}
public class Screen1BHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
createGameScreen2B(); //LINE 225
}
}
}
Stack trace:
Thread [AWT-EventQueue-0] (Suspended)
The_Room.createGameScreen2B() line: 190
The_Room$Screen1BHandler.actionPerformed(ActionEvent) line: 225
JButton(AbstractButton).fireActionPerformed(ActionEvent) line: 1967
AbstractButton$Handler.actionPerformed(ActionEvent) line: 2308
DefaultButtonModel.fireActionPerformed(ActionEvent) line: 405
DefaultButtonModel.setPressed(boolean) line: 262
AquaButtonUI$AquaButtonListener(BasicButtonListener).mouseReleased(MouseEvent) line: 279
JButton(Component).processMouseEvent(MouseEvent) line: 6632
JButton(JComponent).processMouseEvent(MouseEvent) line: 3342
JButton(Component).processEvent(AWTEvent) line: 6397
JButton(Container).processEvent(AWTEvent) line: 2263
JButton(Component).dispatchEventImpl(AWTEvent) line: 5008
JButton(Container).dispatchEventImpl(AWTEvent) line: 2321
JButton(Component).dispatchEvent(AWTEvent) line: 4840
LightweightDispatcher.retargetMouseEvent(Component, int, MouseEvent) line: 4918
LightweightDispatcher.processMouseEvent(MouseEvent) line: 4547
LightweightDispatcher.dispatchEvent(AWTEvent) line: 4488
JFrame(Container).dispatchEventImpl(AWTEvent) line: 2307
JFrame(Window).dispatchEventImpl(AWTEvent) line: 2772
JFrame(Component).dispatchEvent(AWTEvent) line: 4840
EventQueue.dispatchEventImpl(AWTEvent, Object) line: 772
EventQueue$4.run() line: 721
EventQueue$4.run() line: 715
AccessController.doPrivileged(PrivilegedAction<T>, AccessControlContext) line: not available [native method]
ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(PrivilegedAction<T>, AccessControlContext, AccessControlContext) line: 85
ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(PrivilegedAction<T>, AccessControlContext) line: 95
EventQueue$5.run() line: 745
EventQueue$5.run() line: 743
AccessController.doPrivileged(PrivilegedAction<T>, AccessControlContext) line: not available [native method]
ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(PrivilegedAction<T>, AccessControlContext, AccessControlContext) line: 85
EventQueue.dispatchEvent(AWTEvent) line: 742
EventDispatchThread.pumpOneEventForFilters(int) line: 203
EventDispatchThread.pumpEventsForFilter(int, Conditional, EventFilter) line: 124
EventDispatchThread.pumpEventsForHierarchy(int, Conditional, Component) line: 113
EventDispatchThread.pumpEvents(int, Conditional) line: 109
EventDispatchThread.pumpEvents(Conditional) line: 101
EventDispatchThread.run() line: 90
Upvotes: 0
Views: 57
Reputation: 46
I see that you already fixed your code but I wanted to let you know that your naming your classes and variables not according to the java conventions. While this doesn't result in compilation errors it does make your code less readable among other java developers and is definitely something you should change if you want to become a professional (java) developer. You're doing it correctly in some places now and incorrectly the next line.
All variable names should be camelCased
. So no underscores. eg goNorthButtonPanel
. We only use underscores in constants (static final) and those should be all uppercase (e.g. SOME_CONSTANT_VALUE
)
Class names should always start with a capital and also be camelcased , so no underscores (TheRoom).
All your methods names are correct so keep up the good work :)
For an overview of all the java conventions you can look at https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html
Upvotes: 1
Reputation: 114
go_northButtonPanel is not initialized until you press Go North button
Upvotes: 0