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

public class JCalculator implements ActionListener {

private JButton[] buttons;
private JLabel display;
private String[] button_Shapes;
//private String s0, s1, s2;

//Create new form Calculator
public JCalculator(){

    //Create new JFrame container
    JFrame jfrm = new JFrame("Calculator");

    //Set the initial size for frame
    jfrm.setSize(500,500);

    //Terminate the program when the user closes the application.
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Set the calculator to center on the screen
    jfrm.setLocationRelativeTo(null);

    //Set the icon
    ImageIcon icon = new ImageIcon("C:/Users/zetsu/Desktop/JCalculator.png");
    jfrm.setIconImage(icon.getImage());

    //Create label
    display = new JLabel("0", JLabel.RIGHT); 

    //Put border around display label
    display.setBorder(BorderFactory.createLineBorder(Color.black));

    //Create a grid layout
    GridLayout layout = new GridLayout(4,4);

    //Create a panel and set layout
    JPanel bottom_Panel = new JPanel();
    bottom_Panel.setLayout(layout);

    //Create an array of buttons
    buttons = new JButton[16];        

    //Put buttons in an array
    String[] button_Shapes = {"7", "8", "9", "/", "4", "5", "6", "x", "1",
                     "2", "3", "-", "0", "C", "=", "+"};

    for(int i = 0; i < button_Shapes.length; i++){
        //make new button name
        JButton btn = new JButton("" + button_Shapes[i]);
        buttons[i] = btn;
        //add action listener for each button
        btn.addActionListener(this);
        //add each button to panel
        bottom_Panel.add(btn);
    }

    //Set [=] button to default
    jfrm.getRootPane().setDefaultButton(buttons[14]);

    //Set Mnemonic to Alt+[C]
    buttons[13].setMnemonic(KeyEvent.VK_C);

    //Add label to content pane
    jfrm.add(display, BorderLayout.NORTH);

    //Add panel to content pane
    jfrm.add(bottom_Panel, BorderLayout.CENTER);

    jfrm.setVisible(true);

}

@Override
public void actionPerformed(ActionEvent e) {
    //try left operand, operation, right operand
    Object event = e.getSource();
    if(event == buttons[0]){
        display.setText(""+button_Shapes[0]);
    }
}

public static void main(String[] args){
    SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            new JCalculator();
        }
    });
}  
}

Program Description:

Implement a simple integer calculator with the following features:

  1. Center the calculator on the default screen at startup.
  2. The display must have a border.
  3. All the operations are in the form: (operand1 operator1 operand2 =) operator2 operand3 =
  4. The [C] button will clear the calculator, eg. initial state. Must support Alt+[C]
  5. Ctrl+[C] Ctrl+button combination will display “(c) your-name” Only the [C] button can be used to restore the current display.
  6. The [=] button is the default button.
  7. The result can be negative.
  8. Only allow up to 8 digits.
  9. Display error such as “Overflow”, “Div by 0”, …, and the [C] button will clear the error and reset the calculator.
  10. Use JCalculator.png as the program icon.

I am trying to write a program to create a swing gui of a simple calculator. I want the number clicked to show up on the label. However, I keep receiving NullPointerException error whenever I click the number. I think the actionPerformed part is wrong but I'm not really sure.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Allen
  • 35
  • 6

1 Answers1

1

You're shadowing the button_Shapes variable.

You declare a instance field called button_Shapes, but in your constructor, you create another variable called button_Shapes which only has context to the constructor (it can only be used within the context of the constructor)

public class JCalculator implements ActionListener {

    private String[] button_Shapes;
    //Create new form Calculator
    public JCalculator(){
        //...    
        //Put buttons in an array
        String[] button_Shapes = {"7", "8", "9", "/", "4", "5", "6", "x", "1",
                     "2", "3", "-", "0", "C", "=", "+"};
        //...

Remove the second declaration

private String[] button_Shapes;
//Create new form Calculator
public JCalculator(){
    //...    
    //Put buttons in an array
    button_Shapes = {"7", "8", "9", "/", "4", "5", "6", "x", "1",
                 "2", "3", "-", "0", "C", "=", "+"};

You might also like to have a read through Language Basics/Variables for some more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • yes that worked. I was thinking that might be the problem but didn't know how to fix it. Thank you! – Allen Sep 27 '18 at 01:12