0
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();
    }

}

So I half copied half made this code on my own. Was just trying out the code to see how it arranges the buttons. I then added the for loop for the last two rows and then i get this error:

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

Don't know how or what is causing this error. Please Help

2
  • Font font = new Font("Comic Sans MS", Font.BOLD, 12); Commented Aug 14, 2014 at 21:01
  • row[6].add(button[i]); Commented Aug 14, 2014 at 21:03

1 Answer 1

4

I see at least one cause for the NullPointerException :

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

You don't initialize row[6].

Here's another one :

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.