0

Hi i get a null pointer exception error at line 114 but i dont know how to solve it. It is a solitaire Peg game with 4 colors and you can switch between the colors. The move where i get the error is from the center (button 24) to the button two steps up it (Button 24 +- 14)

import javax.swing.*;
import java.awt.Color;
import java.awt.event.*;

public class solitair extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;
    private boolean barriere = false;
    private JButton[] board = new JButton[49];
    private JButton reset;
    private String[] Auswahl = { "red", "green", "black" };
    private JRadioButton[] Option = new JRadioButton[3];

    public solitair() {
        super("Solitaire Spiel");
        JPanel Platte = new JPanel();
        Platte.setLayout(null);
        for (int z = 0; z < 7; z++) {
            for (int s = 0; s < 7; s++) {
                board[z + (s * 7)] = new JButton();
                board[z + (s * 7)].setSize(40, 40);
                board[z + (s * 7)].setLocation(70 + (70 * z), 70 + (70 * s));
                board[z + (s * 7)].addActionListener(this);
                Platte.add(board[z + (s * 7)]);
                setContentPane(Platte);
            }
        }
        reset = new JButton("Neues spiel");

        reset.setSize(133, 30);
        reset.setLocation(33, 500);
        reset.addActionListener(this);
        Platte.add(reset);
        ButtonGroup Gruppe = new ButtonGroup();
        for (int c = 0; c < 3; c++) {
            Option[c] = new JRadioButton(Auswahl[c]);
            Option[c].setSize(80, 30);
            Option[c].setLocation(70, 10 + (c * 40));
            Option[c].addActionListener(this);
            Gruppe.add(Option[c]);
            Platte.add(Option[c]);
        }
        Option[0].setSelected(true);
        setContentPane(Platte);
        for (int i = 0; i <= 48; i++) {
            if (i >= 2 && i <= 4)
                board[i].setBackground(Color.blue);
            if (i > 8 && i < 12)
                board[i].setBackground(Color.blue);
            if (i > 13 && i < 35)
                board[i].setBackground(Color.blue);
            if (i == 24)
                board[i].setBackground(Color.gray);
            if (i > 36 && i < 40)
                board[i].setBackground(Color.blue);
            if (i > 43 && i < 47)
                board[i].setBackground(Color.blue);

            board[0].setVisible(false);
            board[1].setVisible(false);
            board[5].setVisible(false);
            board[6].setVisible(false);
            board[7].setVisible(false);
            board[8].setVisible(false);
            board[12].setVisible(false);
            board[13].setVisible(false);
            board[35].setVisible(false);
            board[36].setVisible(false);
            board[40].setVisible(false);
            board[41].setVisible(false);
            board[42].setVisible(false);
            board[43].setVisible(false);
            board[47].setVisible(false);
            board[48].setVisible(false);
        }
    }

    public void actionPerformed(ActionEvent Ereignis) {
        Object Quelle = Ereignis.getSource();
        {
            for (int i = 0; i < 49; i++) {
                if (Quelle == board[i] && board[i].getBackground() == Color.blue && !barriere) {
                    board[i].setBackground(Color.yellow);
                    barriere = true;

                } else if (Quelle == board[i] && board[i].getBackground() == Color.yellow
                        && barriere) {
                    board[i].setBackground(Color.blue);
                    barriere = false;
                }
                if (Quelle == board[i] && board[i].getBackground() == Color.gray
                        && board[i - 1].getBackground() == Color.blue
                        && board[i - 2].getBackground() == Color.yellow) {
                    board[i].setBackground(Color.blue);
                    board[i - 1].setBackground(Color.gray);
                    board[i - 2].setBackground(Color.gray);
                    barriere = false;
                }

                if (Quelle == board[i] && board[i].getBackground() == Color.gray
                        && board[i + 1].getBackground() == Color.blue
                        && board[i + 2].getBackground() == Color.yellow) {
                    board[i].setBackground(Color.blue);
                    board[i + 1].setBackground(Color.gray);
                    board[i + 2].setBackground(Color.gray);
                    barriere = false;
                }
                if (Quelle == board[i] && board[i].getBackground() == Color.gray
                        && board[i - 7].getBackground() == Color.blue
                        && board[i - 14].getBackground() == Color.yellow) {
                    board[i].setBackground(Color.blue);
                    board[i - 7].setBackground(Color.gray);
                    board[i - 14].setBackground(Color.gray);
                    barriere = false;
                }
                if (Quelle == board[i] && board[i].getBackground() == Color.gray
                        && board[i + 7].getBackground() == Color.blue
                        && board[i + 14].getBackground() == Color.yellow) {
                    board[i].setBackground(Color.blue);
                    board[i + 7].setBackground(Color.gray);
                    board[i + 14].setBackground(Color.gray);
                    barriere = false;
                }
                if (Quelle == board[i] && board[i].getBackground() == Color.red && !barriere) {
                    board[i].setBackground(Color.PINK);
                    barriere = true;
                } else if (Quelle == board[i] && board[i].getBackground() == Color.pink
                        && barriere) {
                    board[i].setBackground(Color.red);
                    barriere = false;
                }
                if (Quelle == board[i] && board[i].getBackground() == Color.gray
                        && board[i - 1].getBackground() == Color.red
                        && board[i - 2].getBackground() == Color.pink) {
                    board[i].setBackground(Color.red);
                    board[i - 1].setBackground(Color.gray);
                    board[i - 2].setBackground(Color.gray);
                    barriere = false;
                }
                if (Quelle == board[i] && board[i].getBackground() == Color.gray
                        && board[i + 1].getBackground() == Color.red
                        && board[i + 2].getBackground() == Color.pink) {
                    board[i].setBackground(Color.red);
                    board[i + 1].setBackground(Color.gray);
                    board[i + 2].setBackground(Color.gray);
                    barriere = false;
                }
                if (Quelle == board[i] && board[i].getBackground() == Color.gray
                        && board[i - 7].getBackground() == Color.red
                        && board[i - 14].getBackground() == Color.pink) {
                    board[i].setBackground(Color.red);
                    board[i - 7].setBackground(Color.gray);
                    board[i - 14].setBackground(Color.gray);
                    barriere = false;
                }
                if (Quelle == board[i] && board[i].getBackground() == Color.gray
                        && board[i + 7].getBackground() == Color.red
                        && board[i + 14].getBackground() == Color.pink) {
                    board[i].setBackground(Color.red);
                    board[i + 7].setBackground(Color.gray);
                    board[i + 14].setBackground(Color.gray);
                    barriere = false;
                }
                if (Quelle == board[i] && board[i].getBackground() == Color.green && !barriere) {
                    board[i].setBackground(Color.magenta);
                    barriere = true;
                } else if (Quelle == board[i] && board[i].getBackground() == Color.magenta
                        && barriere) {
                    board[i].setBackground(Color.green);
                    barriere = false;
                }
                if (Quelle == board[i] && board[i].getBackground() == Color.gray
                        && board[i - 1].getBackground() == Color.green
                        && board[i - 2].getBackground() == Color.magenta) {
                    board[i].setBackground(Color.green);
                    board[i - 1].setBackground(Color.gray);
                    board[i - 2].setBackground(Color.gray);
                    barriere = false;
                }
                if (Quelle == board[i] && board[i].getBackground() == Color.gray
                        && board[i + 1].getBackground() == Color.green
                        && board[i + 2].getBackground() == Color.magenta) {
                    board[i].setBackground(Color.green);
                    board[i + 1].setBackground(Color.gray);
                    board[i + 2].setBackground(Color.gray);
                    barriere = false;
                }
                if (Quelle == board[i] && board[i].getBackground() == Color.gray
                        && board[i - 7].getBackground() == Color.green
                        && board[i - 14].getBackground() == Color.magenta) {
                    board[i].setBackground(Color.green);
                    board[i - 7].setBackground(Color.gray);
                    board[i - 14].setBackground(Color.gray);
                    barriere = false;
                }
                if (Quelle == board[i] && board[i].getBackground() == Color.gray
                        && board[i + 7].getBackground() == Color.green
                        && board[i + 14].getBackground() == Color.magenta) {
                    board[i].setBackground(Color.green);
                    board[i + 7].setBackground(Color.gray);
                    board[i + 14].setBackground(Color.gray);
                    barriere = false;
                }
                if (Quelle == board[i] && board[i].getBackground() == Color.black && !barriere) {
                    board[i].setBackground(Color.CYAN);
                    barriere = true;
                } else if (Quelle == board[i] && board[i].getBackground() == Color.cyan
                        && barriere) {
                    board[i].setBackground(Color.black);
                    barriere = false;
                }
                if (Quelle == board[i] && board[i].getBackground() == Color.white
                        && board[i - 1].getBackground() == Color.black
                        && board[i - 2].getBackground() == Color.cyan) {
                    board[i].setBackground(Color.black);
                    board[i - 1].setBackground(Color.white);
                    board[i - 2].setBackground(Color.white);
                    barriere = false;

                }
                if (Quelle == board[i] && board[i].getBackground() == Color.white
                        && board[i + 1].getBackground() == Color.black
                        && board[i + 2].getBackground() == Color.cyan) {
                    board[i].setBackground(Color.black);
                    board[i + 1].setBackground(Color.white);
                    board[i + 2].setBackground(Color.white);
                    barriere = false;
                }
                if (Quelle == board[i] && board[i].getBackground() == Color.white
                        && board[i - 7].getBackground() == Color.black
                        && board[i - 14].getBackground() == Color.cyan) {
                    board[i].setBackground(Color.black);
                    board[i - 7].setBackground(Color.white);
                    board[i - 14].setBackground(Color.white);
                    barriere = false;

                }
                if (Quelle == board[i] && board[i].getBackground() == Color.white
                        && board[i + 7].getBackground() == Color.black
                        && board[i + 14].getBackground() == Color.cyan) {
                    board[i].setBackground(Color.black);
                    board[i + 7].setBackground(Color.white);
                    board[i + 14].setBackground(Color.white);
                    barriere = false;
                }
                if (Quelle == reset) {
                    if (i >= 2 && i <= 4)
                        board[i].setBackground(Color.blue);
                    if (i > 8 && i < 12)
                        board[i].setBackground(Color.blue);
                    if (i > 13 && i < 35)
                        board[i].setBackground(Color.blue);
                    if (i == 24)
                        board[i].setBackground(Color.gray);
                    if (i > 36 && i < 40)
                        board[i].setBackground(Color.blue);
                    if (i > 43 && i < 47)
                        board[i].setBackground(Color.blue);
                }
                for (int c = 0; c < 3; c++) {
                    if (Quelle == Option[c]) {
                        if (c == 0) {
                            if (i >= 2 && i <= 4)
                                board[i].setBackground(Color.red);
                            if (i > 8 && i < 12)
                                board[i].setBackground(Color.red);
                            if (i > 13 && i < 35)
                                board[i].setBackground(Color.red);
                            if (i == 24)
                                board[i].setBackground(Color.gray);
                            if (i > 36 && i < 40)
                                board[i].setBackground(Color.red);
                            if (i > 43 && i < 47)
                                board[i].setBackground(Color.red);
                        }

                        if (c == 1) {
                            if (i >= 2 && i <= 4)
                                board[i].setBackground(Color.green);
                            if (i > 8 && i < 12)
                                board[i].setBackground(Color.green);
                            if (i > 13 && i < 35)
                                board[i].setBackground(Color.green);
                            if (i == 24)
                                board[i].setBackground(Color.gray);
                            if (i > 36 && i < 40)
                                board[i].setBackground(Color.green);
                            if (i > 43 && i < 47)
                                board[i].setBackground(Color.green);
                        }
                        if (c == 2) {
                            if (i >= 2 && i <= 4)
                                board[i].setBackground(Color.black);
                            if (i > 8 && i < 12)
                                board[i].setBackground(Color.black);
                            if (i > 13 && i < 35)
                                board[i].setBackground(Color.black);
                            if (i == 24)
                                board[i].setBackground(Color.white);
                            if (i > 36 && i < 40)
                                board[i].setBackground(Color.black);
                            if (i > 43 && i < 47)
                                board[i].setBackground(Color.black);
                        }
                    }
                }

            }
        }
    }

    // Das Fenster wird geschlossen wenn man auf die X drückt
    public static void main(String[] args) {
        solitair Fenster = new solitair();
        Fenster.setSize(700, 600);
        Fenster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Fenster.setVisible(true);
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    First and foremost, work to get rid of the unnecessary repetition in that code. It makes understanding and debugging difficult for us (and for you). Next understand that the heuristic for debugging a NullPointerException is almost always the same: You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. – Hovercraft Full Of Eels May 07 '17 at 14:35
  • Also, if this were my program, I'd create at least two classes, one for the GUI, and one for the logic that underpins the GUI. – Hovercraft Full Of Eels May 07 '17 at 14:49

0 Answers0