Having issues with Java swing while making a calculator app

59 views Asked by At

I have been working on creating a calculator on and off and I am using a tutorial created by Bro Code. I was able to run it multiple times without issue but after comings back a couple of weeks later, it stopped working on me. I didn't change a line of code, I didn't add anything new. It was once working perfectly and it is now throwing a java.lang.NullPointerException on the line (Calculator calc = new Calculator();)

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

public class Calculator implements ActionListener
{
    JFrame frame;
    JTextField tf; 
    JButton[] numButtons = new JButton[10];
    JButton[] funcButtons = new JButton[7];
    JButton addButton, subButton, mulButton, divButton;
    JButton decButton, eqButton, clrButton;
    JPanel panel;

    Font myFont = new Font("Courier Bold", Font.BOLD, 30);
    
    Calculator()
    {
        frame = new JFrame("Calculator");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(420, 550);
        frame.setLayout(null);

        tf.setBounds(50, 25, 300, 50);
        tf.setFont(myFont);
        tf.setEditable(false);
        //Buttons
        //Creating our function buttons
        addButton = new JButton("+");
        subButton = new JButton("-");
        mulButton = new JButton("x");
        divButton = new JButton("/");
        decButton = new JButton(".");
        clrButton = new JButton("clr");
        eqButton = new JButton("=");

        //Adding function buttons to funcButtons array
        funcButtons[0] = addButton;
        funcButtons[1] = subButton;
        funcButtons[2] = mulButton;
        funcButtons[3] = divButton;
        funcButtons[4] = decButton;
        funcButtons[5] = clrButton;
        funcButtons[6] = eqButton;

        //Adding action listeners and stuff to the func buttons
        for (int i = 0; i < funcButtons.length; i++)
        {
            funcButtons[i].addActionListener(this);
            funcButtons[i].setFont(myFont);
            funcButtons[i].setFocusable(false);
        }

        for (int i = 0; i < numButtons.length; i++)
        {
            numButtons[i] = new JButton(String.valueOf(i));
            numButtons[i].addActionListener(this);
            numButtons[i].setFocusable(false);
        }

        clrButton.setBounds(127, 430, 145, 50);

        panel = new JPanel();
        panel.setBounds(50, 100, 300, 300);
        panel.setLayout(new GridLayout(4,4,10,10));
        //panel.setBackground(Color.gray);
        //Adding Buttons to panel
        panel.add(numButtons[1]);
        panel.add(numButtons[2]);
        panel.add(numButtons[3]);
        panel.add(addButton);
        panel.add(numButtons[4]);
        panel.add(numButtons[5]);
        panel.add(numButtons[6]);
        panel.add(subButton);
        panel.add(numButtons[7]);
        panel.add(numButtons[8]);
        panel.add(numButtons[9]);
        panel.add(mulButton);
        panel.add(decButton);
        panel.add(numButtons[0]);
        panel.add(eqButton);
        panel.add(divButton);

        frame.add(panel);
        frame.add(clrButton);
        frame.add(tf);
        frame.setVisible(true);
    }
    public static void main(String args[]){
        Calculator calc = new Calculator();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        for (int i = 0; i < 10; i++)
        {
            if (e.getSource() == numButtons[i])
            {
                tf.setText(tf.getText().concat(String.valueOf(i)));
            }
        }
        throw new UnsupportedOperationException("Unimplemented method 'actionPerformed'");
    }
}

Sorry this is my first time on stack overflow so I am not sure about posting my entire program, but I want to give as much context as I can.

I just want to figure out why my code isn't running anymore

1

There are 1 answers

1
crimsony On

I found out the culprit lies in the 8th line. see JTextField tf;

You should modify it as JTextField tf = new JTextField();