0

I'm building a small game in an applet. Whenever I try to run it, I get the following error on the line buttons[i][k] = new ActiveSquare(k);

error:

java.lang.NullPointerException
at com.proj3.renee.ClickAid.<init>(ClickAid.java:21)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at java.lang.Class.newInstance(Class.java:379)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:795)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:724)
at sun.applet.AppletPanel.run(AppletPanel.java:380)
at java.lang.Thread.run(Thread.java:745)

I've read up on similar answers and newInstance0 issues and I've checked to make sure the classes within ActiveSquare are working, but I still can't place the problem. Any suggestions or further reading would be appreciated. Here's the first bit of code and the constructor (I can post more if requested):

public class ClickAid extends Applet implements ActionListener {
ActiveSquare[][] buttons;
private static final long serialVersionUID = 1L;

public ClickAid() { 
    setLayout(new GridLayout(0, 2, 5, 5));
    JPanel panel = new JPanel();
    add(panel);

    for (int i = 0; i < 4; ++i){//default = 4
            for (int k = 0; k < 6; ++k){
                    buttons[i][k] = new ActiveSquare(k); //this is where the error is
                    panel.add(buttons[i][k].buttonaspect);
                    buttons[i][k].buttonaspect.addActionListener(this);
        }
    }       
}
r.ree
  • 35
  • 5

1 Answers1

1

You have to initialize the buttons[i] element of the array. buttons[i] = new ActiveSquare[6]

mic4ael
  • 7,974
  • 3
  • 29
  • 42