Hi.

At the moment I'm trying to get this very simple calculator to just set the label of the text in the GUI to just be the label value at the moment. I plan to make it do the math but I'm having trouble getting to that point. When I try the code it compiles fine but when I click the first button I tested with it I get an error called NullPointerException. I understand that it sends this because there is something that is grabbing a null value. I believe it is the action event but I really don't know what else to do to get it to work.

Error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at TheCalc$MyButtonHandler.actionPerformed(TheCalc.java:104)
        at java.awt.Button.processActionEvent(Button.java:392)
        at java.awt.Button.processEvent(Button.java:360)
        at java.awt.Component.dispatchEventImpl(Component.java:4630)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)

        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)

        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Java Code:

import java.awt.Color;
import java.awt.Font;
import java.awt.*;
import java.awt.font.*;
import java.io.*;
import java.awt.event.*;

public class TheCalc extends WindowAdapter
{
	Frame f;
	Button n1,n2,n3,n4,n5,n6,n7,n8,n9,n0,op,os,od,om,oe;
	Panel p;
	Label	lbl;

	public void makeCalc()
	{
	f = new Frame("I wish this was more then a GUI");
	Font sanscrap = new Font("SansSerif", Font.BOLD, 20);

	Label lbl = new Label("0.0",Label.RIGHT);
	lbl.setFont(sanscrap);

	n1 = new Button("1");
	n2 = new Button("2");
	n3 = new Button("3");
	n4 = new Button("4");
	n5 = new Button("5");
	n6 = new Button("6");
	n7 = new Button("7");
	n8 = new Button("8");
	n9 = new Button("9");
	n0 = new Button("0");
	Button op = new Button("+");
	op.setBackground(Color.yellow);
	Button os = new Button("-");
	os.setBackground(Color.yellow);
	Button od = new Button("/");
	od.setBackground(Color.yellow);
	Button om = new Button("*");
	om.setBackground(Color.yellow);
	Button oe = new Button("=");
	oe.setBackground(Color.green);

	p = new Panel();



	f.add(lbl,BorderLayout.NORTH);
	f.add(p,BorderLayout.CENTER);

	p.setLayout(new GridLayout(4,4));
	p.add(n1);
	p.add(n2);
	p.add(n3);
	p.add(n4);
	p.add(n5);
	p.add(n6);
	p.add(n7);
	p.add(n8);
	p.add(n9);
	p.add(n0);
	p.add(op);
	p.add(os);
	p.add(od);
	p.add(om);
	p.add(oe);
	f.setSize(500,500);
	f.setVisible(true);

	f.addWindowListener(this);
	n1.addActionListener(new MyButtonHandler());
	n2.addActionListener(new MyButtonHandler());
	n3.addActionListener(new MyButtonHandler());
	n4.addActionListener(new MyButtonHandler());
	n5.addActionListener(new MyButtonHandler());
	n6.addActionListener(new MyButtonHandler());
	n7.addActionListener(new MyButtonHandler());
	n8.addActionListener(new MyButtonHandler());
	n9.addActionListener(new MyButtonHandler());
	op.addActionListener(new MyButtonHandler());
	os.addActionListener(new MyButtonHandler());
	od.addActionListener(new MyButtonHandler());
	om.addActionListener(new MyButtonHandler());
	oe.addActionListener(new MyButtonHandler());
	}



	public void windowClosing(WindowEvent we)
	{
		System.exit(0);
	}

public class MyButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent ae)
		{
			String label = ae.getActionCommand();

			if(label == "1")
			{
				lbl.setText("1");
			}

		}

}





	public static void main(String args[])
	{
	TheCalc done = new TheCalc();
	done.makeCalc();
	}
}

Anyone got any ideas?

Thanks in advance!

You never initialized the instance variable "lbl" which is declared on line 14 above. At line 21, you declared and initialized a different variable that is also called "lbl". But inside your actionPerformed method, the "lbl" that it is trying to call setText() on is the "lbl" that you declared at line 14, but never initialized (using the new keyword).

if(label == "1")

What you just did in the code above is compared the reference (memory addresses) of label and "1". If you want to check if any two Objects (and String is an Object) are logically equal, then you need to call the equals() method:

if(label.equals("1"))

Btw, I am very curious as to why label == "1" actually returned true. Perhaps the JVM somehow knew that the String Object "1" had already been created so it pooled them together, therefore resulting in the addresses being the same. But if that is actually the case, I doubt you realized it, hence what I pointed out above.

You never initialized the instance variable "lbl" which is declared on line 14 above. At line 21, you declared and initialized a different variable that is also called "lbl". But inside your actionPerformed method, the "lbl" that it is trying to call setText() on is the "lbl" that you declared at line 14, but never initialized (using the new keyword).

if(label == "1")

What you just did in the code above is compared the reference (memory addresses) of label and "1". If you want to check if any two Objects (and String is an Object) are logically equal, then you need to call the equals() method:

if(label.equals("1"))

Ah, I see what you mean, you are right. I took it out of the method and used that label compare and it does exactly what I want. Thanks so much!

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.