java - 对按钮数组实现两个循环后出现 NullPointerException

标签 java swing nullpointerexception

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

public class ScientificCalc extends JFrame implements ActionListener
{

    JPanel[] row = new JPanel[8];
    JPanel[] row2 = new JPanel[3];
    JButton[] button = new JButton[29];
    String[] buttonString = {   "7", "8", "9", "+", "√",
                                "4", "5", "6", "-", "³√",
                                "1", "2", "3", "*", "x²",
                                "0", "/", "C", "±",
                                "sin", "cos", "tan", "=", ".",
                                "x³", "xⁿ", "10ⁿ", "π", "ⁿ√"};
    int[] dimW = {300, 45, 100, 90};
    int[] dimH = {35, 40, 100};
        Dimension displayDimension = new Dimension(dimW[0], dimH[0]);
        Dimension regularDimension = new Dimension(dimW[1], dimH[1]);
        Dimension rColumnDimension = new Dimension(dimW[2], dimH[1]);
        Dimension zeroButDimension = new Dimension(dimW[3], dimH[1]);
    boolean[] function = new boolean[4];
    double[] temporary = {0, 0};
    JTextArea display = new JTextArea (1,20);
    JTextArea funcdisp = new JTextArea(1,20);
    Font font = new Font("Comic Sans MS", Font.BOLD, 12);


    ScientificCalc()
    {
        super("Scientific Calculator");
        setDesign();
        setSize(400, 350);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        GridLayout grid = new GridLayout(6,5);
        setLayout(grid);

        for (int i = 0; i < 4; i++)
        {
            function[i] = false;
        }

        FlowLayout f1 = new FlowLayout(FlowLayout.CENTER);
        FlowLayout f2 = new FlowLayout(FlowLayout.CENTER,1,1);

        for(int i = 0; i < 6; i++)
        {   
            row[i] = new JPanel();
        }

        row[0].setLayout(f1);

        for(int i = 1; i < 6; i++)
        {   
            row[i].setLayout(f2);
        }

        for(int i = 0; i < 29; i++) 
        {
            button[i] = new JButton();
            button[i].setText(buttonString[i]);
            button[i].setFont(font);
            button[i].addActionListener(this);
        }

        display.setFont(font);
        display.setEditable(false);

        funcdisp.setFont(font);
        funcdisp.setEditable(false);

        display.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        display.setPreferredSize(displayDimension);

        funcdisp.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        funcdisp.setPreferredSize(displayDimension);

        for(int i = 0; i < 15; i++)
        {   
            button[i].setPreferredSize(regularDimension);
        }

        for(int i = 15; i < 29; i++)
        {   
            button[i].setPreferredSize(regularDimension);
        }

        button[15].setPreferredSize(zeroButDimension);

        row[0].add(display);
        add(row[0]);

        row[1].add(funcdisp);
        add(row[1]);

        for(int i = 0; i < 5; i++)
        {   
            row[2].add(button[i]);
            add(row[2]);
        }

        for(int i = 5; i < 10; i++)
        {
            row[3].add(button[i]);
            add(row[3]);
        }

        for(int i = 10; i < 15; i++)
        { 
            row[4].add(button[i]);
            add(row[4]);
        }

        row[5].add(button[15]);

        for(int i = 15; i < 19 ; i++)
        {
            row[5].add(button[i]);
            add(row[5]);
        }

        for(int i = 19; i < 24 ; i++)
        {
            row[6].add(button[i]);
            add(row[6]);
        }

        for(int i = 24; i < 29; i++)
        {
            row[7].add(button[i]);
            add(row[7]);    
        }

        setVisible(true);
    }





    public final void setDesign() {
        try {
            UIManager.setLookAndFeel(
                    "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch(Exception e) {   
        }
    }

    public static void main(String[] args) 
    {
        new ScientificCalc();
    }

}

所以我自己一半复制一半编写了这段代码。只是尝试代码以查看它如何排列按钮。然后我为最后两行添加了 for 循环,然后我收到此错误:

Exception in thread "main" java.lang.NullPointerException
at ScientificCalc.<init>(ScientificCalc.java:126)
at ScientificCalc.main(ScientificCalc.java:153)

不知道如何或什么原因导致此错误。请帮忙

最佳答案

我发现 NullPointerException 至少有一个原因:

    for(int i = 0; i < 6; i++)
    {   
        row[i] = new JPanel();
    }
    ...
    row[6].add(button[i]);

您没有初始化row[6]

这是另一个:

row[7].add(button[i]);

关于java - 对按钮数组实现两个循环后出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25317498/

相关文章:

java - Java Swing 中的 Sierpinski Gasket 实现仅有时出现

java - 使用硬编码值,并且仍然会获得java.lang.NullPointerException

java - 空指针异常, "Attempt to read from field on a null object reference"

java - 将 Moxy 设置为默认 jaxb 实现提供者

java - Ant 构建的 WAR 文件中的 Hibernate 配置文件

java - Spring 3.2 ehcache 的缓存 NamespaceHandler

java - 当 JtextField 被填充时,JTable 单元格也必须被填充。怎么做?

java - 使用 XStream 将 XML 转换为 Java Map<String, Integer>

java - JScrollPane:重新绘制离屏缓冲图像

java - 无法在 Eclipse (Mac) 中创建 Hello World,构建错误 (NullPointerException)