Here is my code. It will compile but will not execute. The error is
Exception in thread "main" java.lang.NullPointerException
at Calculator2.<init>(Calculator2.java.33)
at Calculator2.main(Calculator2.java.91)

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

class Calculator2 extends JFrame implements ActionListener {
    private JPanel panelAdder;
    private JLabel labela;
    private JLabel labelt;
    private JLabel labelr;
    private JTextField textFieldAmount;
    private JTextField textFieldTerm;
    private JTextField textFieldRate;
    private JTextField textFieldResult;
    private JTextField textFieldClear;
    private JButton buttonCalc;
    private JButton buttonClear;

    //Clear Fields
    private boolean clearText=true;
    String selection="";


    public Calculator2()
    {
          super("Calculator");
          initComponents();
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setVisible(true);
          pack();

          // Add Listeners
          buttonCalc.addActionListener(this);
          buttonClear.addActionListener(this);

    }
    public void initComponents()
    {
        //Initialize Components
        panelAdder = new JPanel();
        labela = new JLabel("Amount");
        textFieldAmount = new JTextField();
        labelt = new JLabel("Term");
        textFieldTerm = new JTextField();
        labelr = new JLabel("Rate");
        textFieldRate = new JTextField();
        textFieldResult = new JTextField();
        JButton calculate =  new JButton("Calculate");
        textFieldClear = new JTextField("");
        JButton clear = new JButton("Clear");
        JButton exit =  new JButton("Exit");
    }

    public void actionPerformed(ActionEvent event)
    {
        Object source = event.getSource ();

        if (source == buttonCalc);
        {
            setResultValue();
        }
        if (event.getSource() == buttonClear)
        {
            clearText=true;                                //set clear text flag to true for new calculation
            textFieldAmount.setText("");                    //set text field to null string (clear)
            textFieldTerm.setText("");
            textFieldRate.setText("");
            textFieldAmount.requestFocus();                 //return focus to Amount

            //Set Object Attributes
               textFieldResult.setEditable(false);
               textFieldResult.setColumns(8);
               textFieldAmount.setColumns(6);
               textFieldTerm.setColumns(2);
               textFieldRate.setColumns(2);
               Container contentPane = getContentPane();
               contentPane.setLayout(new FlowLayout());
        }
    }
      // add components to the panel
      public void setResultValue()
      {
          double amount = Double.parseDouble (textFieldAmount.getText());
          double rate = Double.parseDouble (textFieldRate.getText());
          double term = Integer.parseInt(textFieldTerm.getText());
          double result = amount * ( rate * Math.pow ( ( 1 + rate ), term ) ) / ( Math.pow( ( 1 + rate ), term ) - 1 );
        textFieldResult.setText(Double.toString(result));
    }

        public static void main(String[] args){ 
        Calculator2 frame = new Calculator2();
    }
}

line 33 is where I tried to initialize the code

textFieldClear = new JTextField("");

line 91 is where I call the Calculator2 in the main method.

I think your trouble is actually these two lines

buttonCalc.addActionListener(this);
          buttonClear.addActionListener(this);

because you never initialize those buttons. There is nothing wrong with the line that you posted.

Correct.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.