Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Part2: actionPerformed : java.lang.NullPointerException 1

Status
Not open for further replies.

asiavoices

Programmer
Sep 20, 2001
90
CA
Hello everyone,

This is followup to my earlier post

I now have the particular section of my actionPerformed working (by printing out... you have click on button 1, 2, or 3, etc.)

but it now seems that when I try to reset or clear a field(s), it is giving me a "java.lang.NullPointerException" X-) error.

I don't understand. Here is my revised code.


public class ItsMe extends JFrame
implements ActionListener
{
......

/* ---- declare JButtons ------- */

private JButton myButton1;
private JButton myButton2;
private JButton myButton3;
private JButton myButton4;

.......



/* ---- My Constructor ------- */

public ItsMe()
{
..........

JTextField nameField = new JTextField(15);
nameField.addActionListener(this);

JTextField costField = new JTextField(8);
costField.addActionListener(this);

JCheckBox taxField = new JCheckBox("Yes", true);
taxField.addActionListener(this);

..........
myButton1 = new JButton("Button 1");
myButton1.addActionListener(this);
...........

myButton2 = new JButton("Button 2");
myButton2.addActionListener(this);
...........

myButton3 = new JButton("Button 3");
myButton3.addActionListener(this);
...........

resetButton = new JButton("Reset");
resetButton.addActionListener(this);
...........

} // end of my constructor




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

//If button 1 pressed . . .

if (source == myButton1)
{
recordCounter ++;
System.out.println("You have clicked on Button 1. Counter hit is: " + recordcounter );
......
}


//If button 2 pressed . . .

if (source == myButton2)
{
recordCounter ++;
System.out.println("You have clicked on Button 2. Counter hit is: " + recordcounter );
......
}


//If button 3 pressed . . .

if (source == myButton3)
{
recordCounter ++;
System.out.println("You have clicked on Button 3. Counter hit is: " + recordcounter );
......
}


//If Reset pressed . . .

if (source == resetButton) :-(
{
nameField.setText("");
costField.setText("");

System.out.println("You have clicked on RESET button. );
......
}

} // end of actionPerformed method


.....


} // end of my ItsMe class


Any ideas?

Thank you,

Christopher



 
Again this is a scope problem. You use the following code to create your fields:
Code:
JTextField nameField = new JTextField(15); 
JTextField costField = new JTextField(8); 
JCheckBox  taxField = new JCheckBox("Yes", true);
These create local instances to the constructor. I assume you have nameField, costField, and taxField as class variables as well. Since your class variables are never initialized then they are null by default, hence the NullPointerException. To fix this, change the initialization lines to:
Code:
nameField = new JTextField(15); 
costField = new JTextField(8); 
taxField = new JCheckBox("Yes", true);
Watch that scope.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top