Question

    // 8x8 shiny button layout
    JButton[][] buttons = new JButton[8][8];
    for (int i=0; i<8; i++) {
        for (int j=0; j<8; j++) {
            buttons[i][j].setLocation(10+j*55, 10+i*55);
            --->buttons[i][j].setSize(69,69);
            int r = 1 + (int)(Math.random()*((7-1)+1));
            buttons[i][j] = new JButton(icons[r]);
            add(buttons[i][j]);

        }

    }

The code above is giving me issue, I keep getting null pointer exception at the code with arrow. I'm new to the forum and java code. Please and thanks for help


I don't know what I did but my code is working now

    // 8x8 shiny button layout
    JButton[][] shinyButton = new JButton[8][8];
    for (int i=0; i<8; i++) {
        for (int j=0; j<8; j++) {
            int r = 1 + (int)(Math.random()*((6-1)+1));
            shinyButton[i][j] = new JButton(icons[r]);
            shinyButton[i][j].setLocation(10+j*69, 10+i*69);
            shinyButton[i][j].setSize(69,69);
            add(shinyButton[i][j]);
        }       
    }

No correct solution

OTHER TIPS

Try to initialize a button first before setting some properties

// 8x8 shiny button layout
JButton[][] buttons = new JButton[8][8];
for (int i=0; i<8; i++) {
    for (int j=0; j<8; j++) {

        /*Initialize a button*/
        int r = 1 + (int)(Math.random()*((7-1)+1));
        buttons[i][j] = new JButton(icons[r]);

        buttons[i][j].setLocation(10+j*55, 10+i*55);
        buttons[i][j].setSize(69,69);      
        add(buttons[i][j]);

    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top