Jericko Ramilo
Jericko Ramilo

Reputation: 70

Exception in thread "AWT-EventQueue-0" help in Java

I keep on getting these errors and I cant figure out why. I know it has something to do with the buttons but I don't know how to fix them.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1090)
    at java.awt.Container.add(Container.java:410)
    at LightsChasingSolver.<init>(LightsChasingSolver.java:45)
    at LightsChasingUI$2.actionPerformed(LightsChasingUI.java:153)

Here is my code

final JFrame frame2 = new JFrame("Lights Chasing Solver");
GridLayout gridLay = new GridLayout(2,1);
GridLayout grid = new GridLayout(5,5);
JPanel mainPanel = new JPanel(grid);        
JPanel legendPanel = new JPanel();

final JButton[][] sht = new JButton[5][5];

for(i=0;i<5;i++){
    for(j=0;j<5; j++) mainPanel.add(sht[i][j]);
}

Upvotes: 2

Views: 208

Answers (1)

JB Nizet
JB Nizet

Reputation: 692043

Every element in your array of buttons is null. You can't add a null component to a container.

for(int i = 0; i < 5; i++) {
    for(int j = 0; j < 5; j++) {
        sht[i][j] = new JButton("Hello"); // add this line
        mainPanel.add(sht[i][j]);
    }
 }

Upvotes: 2

Related Questions