I get a run-time error NullPointerException whenever I try to click the options button on my JFrame. The JFrame is CharSelection(), and I want to disable it so that I can’t click anything on that JFrame and have to click the back button to enable it again. This is the code for the button.
JButton btnOptions = new JButton("Options");
btnOptions.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
CharSelection.disabled();
GameMenu.getMusicOption().setVisible(true);
}
});
btnOptions.setForeground(Color.WHITE);
btnOptions.setFocusPainted(false);
btnOptions.setBackground(Color.BLACK);
btnOptions.setBounds(10, 669, 200, 50);
contentPane.add(btnOptions);
Inside the mouseClicked event I try to disable and then retrieve the other jFrame, I know for a fact that I can retrieve the other JFrame if I take out the disabling line, the method I’m using to disable the JFrame is a method I created in the CharSelection class this is it.
public static void disabled(){
frame.setEnabled(false);
}
I have the frame instantiated as
private static CharSelection frame;
This avoids any compilation errors but whenever I run the program and click this option button with this code nothing happens and I’m given the NullPointerException error. Not sure how to fix this but you should also know that I do the exact same thing in the first Jframe GameMenu. Except it disables the JFrame no problem, I’ve been trying to figure this problem out for a few days now, spending about 4 hours a day purely on this problem and can’t figure it out.
The error code is this
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at game.CharSelection.disabled(CharSelection.java:1925)
at game.CharSelection$34.mouseClicked(CharSelection.java:1748)
at java.awt.AWTEventMulticaster.mouseClicked(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)
With the most important lines being the first three. I can only speculate on what’s causing this and without knowing the cause I have no way to fix it. Help would be appreciated thanks.
Hopefully this will help you see the code, I’ve taken out all the irrelevant stuff.
private static CharSelection frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new CharSelection();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public CharSelection() {
JButton btnOptions = new JButton("Options");
btnOptions.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
CharSelection.disabled();
GameMenu.getMusicOption().setVisible(true);
}
});
btnOptions.setForeground(Color.WHITE);
btnOptions.setFocusPainted(false);
btnOptions.setBackground(Color.BLACK);
btnOptions.setBounds(10, 669, 200, 50);
contentPane.add(btnOptions);
}
public static void disabled(){
frame.setEnabled(false);
}
public static void enabled(){
frame.setEnabled(true);
}
You do not instantiate the frame really. There’s a
missing somewhere.
Plus to do this via
staticmethods looks like very bad practice but it’s hard to suggest a solution without seeing the whole picture.