I can not understand what the problem is ... gives an error on "field.add (panel1)"
private JPanel contentPane; public Parameters Parameters; public JPanel panel1; public JPanel panel2; public JPanel field; public Color rightcolor1; public Color wrongcolor1; public int a; public int b; /** * Launch the application */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { P frame = new P(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. * @return */ public void param(){ boolean[] wrongcolor = new boolean[2]; wrongcolor[0] = Parameters.isG1(); wrongcolor[1] = Parameters.isR1(); if (wrongcolor[0] = true){ wrongcolor1 = Color.GREEN; } else wrongcolor1 = Color.RED; boolean[] rightcolor = new boolean[2]; rightcolor[0] = Parameters.isG2(); rightcolor[1] = Parameters.isR2(); if (rightcolor[0] = true){ rightcolor1 = Color.GREEN; } else rightcolor1 = Color.RED; boolean[] size = new boolean[3]; size[0] = Parameters.isS1(); size[1] = Parameters.isS2(); size[2] = Parameters.isS3(); if (size[0] = true) { a = 1; } else if (size[1] = true) { a = 2; } else a = 3; boolean[] speed = new boolean[3]; speed[0] = Parameters.isSp1(); speed[1] = Parameters.isSp2(); speed[2] = Parameters.isSp3(); if (speed[0] = true) { b = 1; } else if (speed[1] = true) { b = 2; } else b = 3; } public int randomRange(int min, int max){ double rnd = Math.random(); return min + (int)(rnd * ((max - min) + 1)); } public int randomRange (int max){ return randomRange(0, max); } public void addingpanel1() { int max = 100; for (int i = 0; i < max; i++) { panel1 = new JPanel(); panel1.setBackground(rightcolor1); /*Dimension kek = panel1.getPreferredSize(); int h = size.height; int w = size.width; int h = size.height; int w = size.width; */ /*panel1.setSize(a, a);*/ int x = randomRange(getWidth() - a); int y = randomRange(getHeight() - a); panel1.setBounds(x, y, a, a); field.add(panel1); field.setComponentZOrder(panel1, 0); panel1.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg) { if(arg.getButton() != MouseEvent.BUTTON3) return; field.remove(panel1); field.updateUI(); } }); } } public void addingpanel2() { int max = 100; for (int i = 0; i < max; i++) { JPanel panel2 = new JPanel(); panel2.setBackground(wrongcolor1); int x = randomRange(getWidth() - a); int y = randomRange(getHeight() - a); panel2.setBounds(x, y, a, a); field.add(panel2); field.setComponentZOrder(panel2, 0); panel2.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg) { if(arg.getButton() != MouseEvent.BUTTON3) return; String s = "Ты проиграл!"; JLabel lose = new JLabel(s); lose.setForeground(Color.RED); lose.setFont(new Font("Tahoma", Font.PLAIN, 25)); lose.setHorizontalAlignment(SwingConstants.CENTER); lose.setBounds(0, 66, 434, 114); field.add(lose); } }); } } public P() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JPanel field = new JPanel(); field.setBounds(0, 26, 434, 205); contentPane.add(field); try { Parameters = new Parameters(this); Parameters.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); } catch (Exception e) { e.printStackTrace(); } JButton newgame = new JButton("\u041D\u043E\u0432\u0430\u044F \u0438\u0433\u0440\u0430"); newgame.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { Parameters.setVisible(true); } }); newgame.setBounds(0, 0, 101, 23); contentPane.add(newgame); JButton statistika = new JButton("\u0421\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043A\u0430"); statistika.setBounds(333, 0, 101, 23); contentPane.add(statistika); JButton info = new JButton("\u041E \u043F\u0440\u043E\u0433\u0440\u0430\u043C\u043C\u0435"); info.setBounds(333, 238, 101, 23); contentPane.add(info); JButton exit = new JButton("\u0412\u044B\u0445\u043E\u0434"); exit.setBounds(234, 238, 89, 23); contentPane.add(exit); JLabel proshlo = new JLabel("\u041F\u0440\u043E\u0448\u043B\u043E \u0432\u0440\u0435\u043C\u0435\u043D\u0438:"); proshlo.setBounds(10, 238, 107, 23); contentPane.add(proshlo); JLabel timer = new JLabel(""); timer.setBounds(119, 238, 105, 23); contentPane.add(timer); param(); addingpanel1(); addingpanel2(); } }
fieldobject in classP, and you refer to a field of a class with the same name, an object of which is not created. When creating an instance offieldin classPremove theJPanelat the beginning of the line by creating a class field, not a local variable - pavlofff