为按钮的数组实现两个循环后获取NullPointerException

时间:2014-08-14 20:58:12

标签: 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)

不知道导致此错误的方式或原因。请帮忙

1 个答案:

答案 0 :(得分:4)

我至少看到NullPointerException的一个原因:

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

您没有初始化row[6]

这是另一个:

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