0

I have been trying to create a JFrame Calculator but am getting the error java.lang.nullpointer.exception. It says that I have a problem in the line where I say:

GuiCalc go = new GuiCalc();

Here is all of the code:

package home.personalprojects.jordan;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class GuiCalc extends JFrame
{

    private JButton calculate;
    private JTextField num1field, num2field;
    private JComboBox operationbox;
    private JLabel label1, label2, label3;
    private String[] operationposs = {"+", "-", "*", "/"};
    String operation;
    int num1, num2, answer;

    public GuiCalc(){

        super("Calculator");
        setLayout(new FlowLayout());

        operationbox = new JComboBox(operationposs);

        calculate = new JButton("Calculate");
        calculate.setToolTipText("Enter Two Numbers And Then Select An Operation To Find An Answer");

        label1 = new JLabel("Number 1: "); 
        num1field = new JTextField("", 10);
        label2 = new JLabel("Number 2: ");
        num2field = new JTextField("", 10);

        operationbox.addItemListener(
            new ItemListener(){
                public void itemStateChanged(ItemEvent event){
                    if(event.getStateChange() == ItemEvent.SELECTED){

                        int temp;
                        temp = operationbox.getSelectedIndex();
                        switch(temp){
                        case 0:
                            answer = num1 + num2;
                        case 1:
                            answer = num1 - num2;
                        case 2:
                            answer = num1 * num2;
                        case 3:
                            answer = num1 / num2;

                    }
                }
            }
        });


        calculate.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    JOptionPane.showMessageDialog(null, String.format("The Answer: " + answer, event.getActionCommand()));
                }
            }
        );

        add(label1);
        add(num1field);
        add(label2);
        add(operationbox);
        add(label3);
        add(num2field);
        add(calculate);

    }

    public static void main(String[] args)
    {

        GuiCalc go = new GuiCalc();
        go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        go.setSize(500,500);
        go.setVisible(true);


    }

}
2
  • Please paste the whole stack trace, along with what the line numbers correspond to. Commented Aug 20, 2013 at 2:16
  • It should say additional line number in method GuiCalc() Commented Aug 20, 2013 at 2:18

3 Answers 3

2

When I run your code I get...

Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1086)
    at java.awt.Container.add(Container.java:998)
    at javax.swing.JFrame.addImpl(JFrame.java:562)
    at java.awt.Container.add(Container.java:410)
    at testcalc.TestCalc.<init>(TestCalc.java:79)
    at testcalc.TestCalc.main(TestCalc.java:87)

Which points to

add(label3);

Which suggest that label3 has not being initialised.

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

3 Comments

Even label3 is null, will it give an exception of nullpointer?
Yes, how could you possibly add a null value to a container? It doesn't make sense (to me anyway)
I did that and now it is giving me the same error message except for the bottom line which is now gone
0

As MadProgrammer says, you haven't initialized label3 by the time you've called add. The JFrame inherits from java.awt.Component, whose add(Compontent comp) method throws an exception if passed a null argument. Make sure to construct the JLabel before adding it.

Comments

0

You need to assign a value to label3 before you add it to this:

label3 = new JLabel("");

or

label3 = null;
if (label3 != null) this.add(label3);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.