Hello. Here I try to deal with arrays. The idea is to create a cycle of 10 buttons, and place them on the panel. Buttons are stored in an array. On startup, knocks out a NullPointerException. Help me to understand)

package trening; import java.awt.BorderLayout; import java.awt.GridLayout; import javax.swing.*; public class massiv { JPanel window; JButton squares []; JPanel p1; massiv () { p1 = new JPanel(); squares = new JButton [10]; for (int i=0; i<10; i++) { squares[i] = new JButton(); squares[i].setVisible (true); p1.setLayout(new GridLayout (4,3)); p1.add(squares[i]); } window.setLayout(new BorderLayout()); window.add("North", p1); JFrame frame = new JFrame("massiv"); frame.setContentPane(window); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { // TODO Auto-generated method stub massiv mas = new massiv(); } } 

    1 answer 1

    It seems that the JPanel window is not initialized. Must be somewhere in the code window = new JPanel ();

    • for sure) thanks)) now we will insert the names on the buttons) - ksaireks
    • You 're welcome :) - Yuri_Prime