As a beginner in Java i try to create a Window Application "Game" with the help of windowsbuilder (eclipse plugin). Now as i try to change The label in an extern function (allblack()) the console gives out the following errors when i press the specific button (The window gets displayed correctly):
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Leiterspiel.allblack(Leiterspiel.java:81)
at Leiterspiel$2.mouseClicked(Leiterspiel.java:234)
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.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(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)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Leiterspiel.allblack(Leiterspiel.java:81)
at Leiterspiel$2.mouseClicked(Leiterspiel.java:234)
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.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(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)
I'd be greatful if anyone could explain it to me as the same code as in "allblack()" works in "inizialize()". Thanks
[Pre-sampled import]
public void allblack()
{
l1.setForeground(Color.black);
l2.setForeground(Color.black);
}
public void initialize() {
frmDasLeiterspiel = new JFrame();
frmDasLeiterspiel.setResizable(false);
frmDasLeiterspiel.setTitle("Das Leiterspiel");
frmDasLeiterspiel.setBounds(100, 100, 420, 420);
frmDasLeiterspiel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmDasLeiterspiel.getContentPane().setLayout(null);
JLabel l1 = new JLabel("1");
l1.setBackground(Color.LIGHT_GRAY);
l1.setBounds(340, 190, 50, 50);
l1.setHorizontalAlignment(SwingConstants.CENTER);
frmDasLeiterspiel.getContentPane().add(l1, BorderLayout.CENTER);
JLabel l2 = new JLabel("2");
l2.setBackground(Color.LIGHT_GRAY);
l2.setHorizontalAlignment(SwingConstants.CENTER);
l2.setBounds(278, 190, 50, 50);
frmDasLeiterspiel.getContentPane().add(l2);
[....]
JButton btnNewButton = new JButton("Roll the dice!");
btnNewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
allblack();
}
});
btnNewButton.setBounds(12, 294, 378, 25);
frmDasLeiterspiel.getContentPane().add(btnNewButton);
[...]
}
it looks like the variables l1 and l2 only exitst in the initialize method. try this: