Hello,
I am using Swing to make a simple GUI but when I add a change listener to a JSlider, I get the following runtime error:
Exception in thread "main" java.lang.NullPointerException
at XMovePanel.<init>(XMovePanel.java:15)
My code is the following:
public class XMovePanel extends JPanel
{
JSlider xCoord;
private GUIApp d;
private XMoveListener xmove;
public XMovePanel(GUIApp d)
{
this.d = d;
xmove = new XMoveListener();
// Error occurs here:
xCoord.addChangeListener(xmove);
// Settings for the slider
private class XMoveListener implements ChangeListener{
@Override
public void stateChanged(ChangeEvent event){
// Change listener does stuff on action here
But I don't know why I would get an error when I add the change listener. What am I doing wrong?