import javax.swing.*;
import java.awt.GridLayout;
import java.awt.event.*;
import java.text.*;


public class TFE extends JFrame implements ActionListener
{
	
	
	public TFE()
	{
		str txtfNum1,txtfNum2,lblAnswer;
		 
		setLayout(new GridLayout(3,2,5,5));
		add(new JLabel("First Number:"));
		add(txtfNum1);
		add(new JTextField(10));
		add(new JLabel("Second Number:"));
		add(txtfNum2);
		add(new JTextField(10));
		add(new JLabel("Total Summation:"));
		add(lblAnswer);
		txtfNum1.addActionListener(this);
		txtfNum2.addActionListener(this);
		
	}
	public void actionPerformed(ActionEvent e)
	{
		String str;
		int val1,val2,total;
		str=txtfNum1.getText();
		if(str.equals(""))
		val1=0;
		
		else
			val1=Integer.parseInt(str);
		str=txtfNum2.getText();
		if(str.equals(""))
		val2=0;
		
		else
			val2=Integer.parseInt(str);
			total=val1+val2;
			lblAnswer.setText(""+total);	
		
	}
	
	public static void main(String[]args){
		
		
		TFE frame=new TFE();
		frame.setTitle("TextField Event");
	frame.setSize(300,350);
	frame.setLocationRelativeTo(null);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setVisible(true);
	}
}

i am confused with the use of parsing...please help me to correct the program...i am new in java...
the errors generated are
--------------------Configuration: <Default>--------------------
C:\Documents and Settings\Administrator\Desktop\TFE.java:14: cannot find symbol
symbol : class str
location: class TFE
str txtfNum1,txtfNum2,lblAnswer;
^
C:\Documents and Settings\Administrator\Desktop\TFE.java:33: cannot find symbol
symbol : variable txtfNum1
location: class TFE
str=txtfNum1.getText();
^
C:\Documents and Settings\Administrator\Desktop\TFE.java:39: cannot find symbol
symbol : variable txtfNum2
location: class TFE
str=txtfNum2.getText();
^
C:\Documents and Settings\Administrator\Desktop\TFE.java:46: cannot find symbol
symbol : variable lblAnswer
location: class TFE
lblAnswer.setText(""+total);
^
4 errors

Process completed.

str txtfNum1,txtfNum2,lblAnswer;

did you mean String ?

yes...then???????

Reading it a agin with my glasses on, you seem to want to use txtfNum1,txtfNum2,lblAnswer as fields on your form, so maybe you should declare them as JTextField

i know my mistake already.thanks....

hey...got another problem..it can compile but cant execute????
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1027)
at java.awt.Container.add(Container.java:935)
at javax.swing.JFrame.addImpl(JFrame.java:545)
at java.awt.Container.add(Container.java:352)
at TFE.<init>(TFE.java:18)
at TFE.main(TFE.java:53)

Process completed.
what is this statement mean????please tell me directly...i am very stupid in java programming...

Nobody know the mistake???????????????
please help me.......

Maybe you declared, but didn't initialise, your new textfield variables - you need something like

JTextField txtfNum1 = new JTextField ();

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.