It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Java Forum

Please Help with ActionListener bug

Please Help with ActionListener bug

Postby joslin01 on Mon Apr 23, 2007 9:13 pm

Hi, I've been staring at this for a long time and don't see the error, whenever I hit a number it gives me the error below the code:


import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JPanel;
import javax.swing.JMenuItem;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Calculator extends JFrame
{
public static final int WIDTH = 500;
public static final int HEIGHT = 500;

private JPanel textPanel;
private JPanel numbersPanel;
private JPanel operandsPanel;
private JTextField textField;

private class MenuListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
textField.setText("0");
}
}

private class NumbersListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String buttonString = e.getActionCommand();

if (buttonString.equals("1"))
textField.setText("1");
if (buttonString.equals("2"))
textField.setText("2");
if (buttonString.equals("3"))
textField.setText("3");
if (buttonString.equals("4"))
textField.setText("4");
if (buttonString.equals("5"))
textField.setText("5");
if (buttonString.equals("6"))
textField.setText("6");
if (buttonString.equals("7"))
textField.setText("7");
if (buttonString.equals("8"))
textField.setText("8");
if (buttonString.equals("9"))
textField.setText("9");
if (buttonString.equals("0"))
textField.setText("0");
}
}


private class OperandsListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}



public static void main(String[] args)
{
Calculator gui = new Calculator();
gui.setVisible(true);
}

public Calculator()
{
super(); //invokes the JFrame constructor.
setSize(WIDTH,HEIGHT);
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //default exit on close

JMenu menu = new JMenu("Options");
JMenuItem clear = new JMenuItem("Clear");
clear.addActionListener(new MenuListener());
menu.add(clear);


JPanel textPanel = new JPanel();
JTextField textField = new JTextField("0", 20);
textPanel.add(textField);

JPanel numbersPanel = new JPanel();
numbersPanel.setLayout(new GridLayout(4,3));

JButton oneButton = new JButton("One");
oneButton.setBackground(Color.GRAY);
numbersPanel.add(oneButton);
oneButton.addActionListener(new NumbersListener());
JButton twoButton = new JButton("2");
twoButton.setBackground(Color.GRAY);
numbersPanel.add(twoButton);
twoButton.addActionListener(new NumbersListener());
JButton threeButton = new JButton("3");
threeButton.setBackground(Color.GRAY);
numbersPanel.add(threeButton);
threeButton.addActionListener(new NumbersListener());
JButton fourButton = new JButton("4");
fourButton.setBackground(Color.GRAY);
numbersPanel.add(fourButton);
fourButton.addActionListener(new NumbersListener());
JButton fiveButton = new JButton("5");
fiveButton.setBackground(Color.GRAY);
numbersPanel.add(fiveButton);
fiveButton.addActionListener(new NumbersListener());
JButton sixButton = new JButton("6");
sixButton.setBackground(Color.GRAY);
numbersPanel.add(sixButton);
sixButton.addActionListener(new NumbersListener());
JButton sevenButton = new JButton("7");
sevenButton.setBackground(Color.GRAY);
numbersPanel.add(sevenButton);
sevenButton.addActionListener(new NumbersListener());
JButton eightButton = new JButton("8");
eightButton.setBackground(Color.GRAY);
numbersPanel.add(eightButton);
eightButton.addActionListener(new NumbersListener());
JButton nineButton = new JButton("9");
nineButton.setBackground(Color.GRAY);
numbersPanel.add(nineButton);
nineButton.addActionListener(new NumbersListener());
JButton zeroButton = new JButton("0");
zeroButton.setBackground(Color.GRAY);
numbersPanel.add(zeroButton);
zeroButton.addActionListener(new NumbersListener());

JPanel operandsPanel = new JPanel();
operandsPanel.setLayout(new GridLayout(4,1));

JButton timesButton = new JButton("*");
operandsPanel.add(timesButton);
timesButton.setBackground(Color.LIGHT_GRAY);
timesButton.addActionListener(new OperandsListener());
JButton divideButton = new JButton("/");
operandsPanel.add(divideButton);
divideButton.setBackground(Color.LIGHT_GRAY);
divideButton.addActionListener(new OperandsListener());
JButton subtractButton = new JButton("-");
operandsPanel.add(subtractButton);
subtractButton.setBackground(Color.LIGHT_GRAY);
subtractButton.addActionListener(new OperandsListener());
JButton addButton = new JButton("+");
operandsPanel.add(addButton);
addButton.setBackground(Color.LIGHT_GRAY);
addButton.addActionListener(new OperandsListener());

add(textPanel, BorderLayout.NORTH);
add(numbersPanel, BorderLayout.CENTER);
add(operandsPanel, BorderLayout.EAST);
add(menu, BorderLayout.WEST);

}



}




Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Calculator$NumbersListener.actionPerformed(Calculator.java:55)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
joslin01
 
Posts: 3
Joined: Mon Apr 23, 2007 9:09 pm

Postby ggomez on Tue Apr 24, 2007 3:26 am

erase this--"JTextField"-- textField = new JTextField("0", 20);
ggomez
 
Posts: 45
Joined: Tue Apr 17, 2007 3:30 am
Location: !!!!!MéXiCo¡¡¡¡¡¡

Postby joslin01 on Tue Apr 24, 2007 1:18 pm

appreciated, however an identity expected error trying to do textField = new JTextField("0",20);

However, JTextField textField = new JTextField("0",20); works and stops giving all those errors - but then the calculator's number isn't changing.
joslin01
 
Posts: 3
Joined: Mon Apr 23, 2007 9:09 pm

This works

Postby ggomez on Tue Apr 24, 2007 5:16 pm

I tried this in my computer and worked fine and the numbers were changing very well.



Code: Select all
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JPanel;
import javax.swing.JMenuItem;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Calculator extends JFrame
{
public static final int WIDTH = 500;
public static final int HEIGHT = 500;

private JPanel textPanel;
private JPanel numbersPanel;
private JPanel operandsPanel;
private JTextField textField;

private class MenuListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
textField.setText("0");
}
}

private class NumbersListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String buttonString = e.getActionCommand();

if (buttonString.equals("1"))
textField.setText("1");
if (buttonString.equals("2"))
textField.setText("2");
if (buttonString.equals("3"))
textField.setText("3");
if (buttonString.equals("4"))
textField.setText("4");
if (buttonString.equals("5"))
textField.setText("5");
if (buttonString.equals("6"))
textField.setText("6");
if (buttonString.equals("7"))
textField.setText("7");
if (buttonString.equals("8"))
textField.setText("8");
if (buttonString.equals("9"))
textField.setText("9");
if (buttonString.equals("0"))
textField.setText("0");
}
}


private class OperandsListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}



public static void main(String[] args)
{
Calculator gui = new Calculator();
gui.setVisible(true);
}

public Calculator()
{
super(); //invokes the JFrame constructor.
setSize(WIDTH,HEIGHT);
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //default exit on close

JMenu menu = new JMenu("Options");
JMenuItem clear = new JMenuItem("Clear");
clear.addActionListener(new MenuListener());
menu.add(clear);


JPanel textPanel = new JPanel();
textField = new JTextField("0", 20);   // i put out the "JTextField" identifier from here
textPanel.add(textField);

JPanel numbersPanel = new JPanel();
numbersPanel.setLayout(new GridLayout(4,3));

JButton oneButton = new JButton("One");
oneButton.setBackground(Color.GRAY);
numbersPanel.add(oneButton);
oneButton.addActionListener(new NumbersListener());
JButton twoButton = new JButton("2");
twoButton.setBackground(Color.GRAY);
numbersPanel.add(twoButton);
twoButton.addActionListener(new NumbersListener());
JButton threeButton = new JButton("3");
threeButton.setBackground(Color.GRAY);
numbersPanel.add(threeButton);
threeButton.addActionListener(new NumbersListener());
JButton fourButton = new JButton("4");
fourButton.setBackground(Color.GRAY);
numbersPanel.add(fourButton);
fourButton.addActionListener(new NumbersListener());
JButton fiveButton = new JButton("5");
fiveButton.setBackground(Color.GRAY);
numbersPanel.add(fiveButton);
fiveButton.addActionListener(new NumbersListener());
JButton sixButton = new JButton("6");
sixButton.setBackground(Color.GRAY);
numbersPanel.add(sixButton);
sixButton.addActionListener(new NumbersListener());
JButton sevenButton = new JButton("7");
sevenButton.setBackground(Color.GRAY);
numbersPanel.add(sevenButton);
sevenButton.addActionListener(new NumbersListener());
JButton eightButton = new JButton("8");
eightButton.setBackground(Color.GRAY);
numbersPanel.add(eightButton);
eightButton.addActionListener(new NumbersListener());
JButton nineButton = new JButton("9");
nineButton.setBackground(Color.GRAY);
numbersPanel.add(nineButton);
nineButton.addActionListener(new NumbersListener());
JButton zeroButton = new JButton("0");
zeroButton.setBackground(Color.GRAY);
numbersPanel.add(zeroButton);
zeroButton.addActionListener(new NumbersListener());

JPanel operandsPanel = new JPanel();
operandsPanel.setLayout(new GridLayout(4,1));

JButton timesButton = new JButton("*");
operandsPanel.add(timesButton);
timesButton.setBackground(Color.LIGHT_GRAY);
timesButton.addActionListener(new OperandsListener());
JButton divideButton = new JButton("/");
operandsPanel.add(divideButton);
divideButton.setBackground(Color.LIGHT_GRAY);
divideButton.addActionListener(new OperandsListener());
JButton subtractButton = new JButton("-");
operandsPanel.add(subtractButton);
subtractButton.setBackground(Color.LIGHT_GRAY);
subtractButton.addActionListener(new OperandsListener());
JButton addButton = new JButton("+");
operandsPanel.add(addButton);
addButton.setBackground(Color.LIGHT_GRAY);
addButton.addActionListener(new OperandsListener());

add(textPanel, BorderLayout.NORTH);
add(numbersPanel, BorderLayout.CENTER);
add(operandsPanel, BorderLayout.EAST);
add(menu, BorderLayout.WEST);

}



}
ggomez
 
Posts: 45
Joined: Tue Apr 17, 2007 3:30 am
Location: !!!!!MéXiCo¡¡¡¡¡¡

Postby joslin01 on Tue Apr 24, 2007 6:05 pm

Oh! I thought you wanted me to get rid of JTextField textField up top. Thanks a lot for your help. Greatly appreciated.
joslin01
 
Posts: 3
Joined: Mon Apr 23, 2007 9:09 pm


Who is online

Users browsing this forum: No registered users and 5 guests