这段代码有什么问题? (JAVA 中的计算器)

Whats wrong with this code ? (Caulculator in JAVA)

我在 youtube 上看到了一个教程并尝试按照视频进行操作, 我完成了计算器按钮的构建,但我的问题是 ECLIPSE 没有 告诉我到底是什么问题,在哪一行...

有人可以帮我处理这段代码吗?

上面写的是这样的:

Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1090)
at java.awt.Container.add(Container.java:966)
at Calculator.Calculator.<init>(Calculator.java:130)
at Calculator.Calculator.main(Calculator.java:146)

代码:

  package Calculator;

  import java.awt.BorderLayout;
  import java.awt.Dimension;
  import java.awt.GridBagConstraints;
  import java.awt.GridBagLayout;
  import java.awt.Insets;

  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JPanel;


   public class Calculator extends JPanel {

    public static final long serialVersionUID = 1L;

    public static final int WIDTH = 320;

    public static final int HEIGHT = 480;

    private GridBagLayout layout;
    private GridBagConstraints gbc;


    private JButton[] numberButtons;

    private JButton[] opButtons;



    private int[][] numConstraints = new int[][] {

                    {0, 5, 2, 1},
                    {0, 4, 1, 1},
                    {1, 4, 1, 1},
                    {2, 4, 1, 1},
                    {0, 3, 1, 1},
                    {1, 3, 1, 1},
                    {2, 3, 1, 1},
                    {0, 2, 1, 1},
                    {1, 2, 1, 1},
                    {2, 2, 1, 1},

    };



   private int[][] opConstraints = new int[][] {

     {2, 5, 1, 1},
     {3, 4, 1, 2},
       {3, 3, 1, 1},
       {3, 2, 1, 1},
      {3, 1, 1, 1},
       {2, 1, 1, 1},
      {1, 1, 1, 1},
        {0, 1, 1, 1},


    };




    public Calculator(){

            setPreferredSize(new Dimension(WIDTH,HEIGHT));

            layout = new GridBagLayout();
            layout.columnWidths = new int[] {80, 80, 80, 80};
            layout.rowHeights = new int[] {80, 80, 80, 80, 80, 80};
            setLayout(layout);



            gbc = new GridBagConstraints();

            numberButtons = new JButton[10];



            for(int i = 0; i < numberButtons.length; i++)

            {

                    numberButtons[i] = new JButton("" + i);

                    gbc.gridx =     numConstraints[i][0];
                    gbc.gridy = numConstraints[i][1];
                    gbc.gridwidth = numConstraints[i][2];
                    gbc.gridheight = numConstraints[i][3];
                    gbc.fill = GridBagConstraints.BOTH;
                    gbc.insets = new Insets(2, 2, 2, 2);

                    add(numberButtons[i], gbc);
            }


          opButtons = new JButton[8];


          opButtons[0] = new JButton(".");
        opButtons[1] = new JButton("=");
        opButtons[2] = new JButton("+");
          opButtons[3] = new JButton("-");
        opButtons[4] = new JButton("*");
          opButtons[5] = new JButton("/");
          opButtons[6] = new JButton("c");
          opButtons[6] = new JButton("+/-");



       for(int i = 0; i < opButtons.length; i++)

        {


                 gbc.gridx =     opConstraints[i][0];
                gbc.gridy = opConstraints[i][1];
                 gbc.gridwidth = opConstraints[i][2];
               gbc.gridheight = opConstraints[i][3];

              add(opButtons[i],gbc);
        }




    }

    public static void main(String[] args){

    JFrame frame = new JFrame("Calculator");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setLayout(new BorderLayout());
    frame.add(new Calculator(), BorderLayout.CENTER);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);


}

}

谢谢!

opButtons[6] = new JButton("+/-");应该使用索引 7。

下面的循环将添加空值 opButtons [7] 导致异常。