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!