Results 1 to 4 of 4
Thread: null pointer exception
- 04-28-2011, 07:10 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
null pointer exception
Can someone help me solve this error please? After entering data into the textboxes , I get null pointer errors. I would appreciate any suggestions.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
public class NewClass5 extends JPanel implements ActionListener {
JTextField txInMortAmt = null;
JTextField txInTerm = null;
JTextField txInRate = null;
JLabel LabInMortAmt = null;
JLabel LabInTerm = null;
JLabel LabInRate= null;
public NewClass5()
{
JLabel LabInMortAmt = new JLabel("Mortgage Amount");
JTextField txInMortAmt = new JTextField(15);
JLabel LabInTerm = new JLabel("Mortgage Term");
JTextField txInTerm = new JTextField(15);
JLabel LabInRate = new JLabel("Interest Rate?");
JTextField txInRate = new JTextField(15);
add(LabInMortAmt);
add(txInMortAmt);
add(LabInTerm);
add(txInTerm);
add(LabInRate);
add(txInRate);
txInMortAmt.addActionListener(this);
txInTerm.addActionListener(this);
txInRate.addActionListener(this);
}
// Show text when user presses ENTER.
public void actionPerformed(ActionEvent ae) {
System.out.println(txInMortAmt.getText());
System.out.println(txInTerm.getText());
System.out.println(txInRate.getText());
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new NewClass5());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setSize(200, 200);
frame.setVisible(true);
}
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at NewClass5.actionPerformed(NewClass5.java:57)[/COLOR]
- 04-28-2011, 07:12 PM #2
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
Please use code tags, and further more which is the like 57?
Measuring programming progress by lines of code is like measuring aircraft building progress by weight.
- 04-28-2011, 07:33 PM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Yes, to use code tags look below
[code ]<-omit space
YOUR CODE HERE
[/code]
And pointing out the line that causes the error is helpful as well.
- 04-28-2011, 08:31 PM #4
Member
- Join Date
- Apr 2011
- Posts
- 34
- Rep Power
- 0
The problem is that you are declaring your members...
...but never assigning anything to them. The reason you are never assigning anything to them is because you are declaring your local variables with the same name...Java Code:JTextField txInMortAmt = null; JTextField txInTerm = null; JTextField txInRate = null;
... and incorrectly assuming that your members are being initialized. All you need to do is simply change your code to this...Java Code:JLabel LabInMortAmt = new JLabel("Mortgage Amount"); JTextField txInMortAmt = new JTextField(15); JLabel LabInTerm = new JLabel("Mortgage Term"); JTextField txInTerm = new JTextField(15); JLabel LabInRate = new JLabel("Interest Rate?"); JTextField txInRate = new JTextField(15);
Java Code:JLabel LabInMortAmt = new JLabel("Mortgage Amount"); txInMortAmt = new JTextField(15); JLabel LabInTerm = new JLabel("Mortgage Term"); txInTerm = new JTextField(15); JLabel LabInRate = new JLabel("Interest Rate?"); txInRate = new JTextField(15);
Similar Threads
-
null pointer exception
By marvelk in forum Advanced JavaReplies: 8Last Post: 02-01-2011, 09:02 AM -
Null pointer exception
By izzy in forum New To JavaReplies: 5Last Post: 03-22-2010, 05:19 PM -
Null Pointer Exception
By andre1011 in forum Advanced JavaReplies: 4Last Post: 02-07-2009, 03:30 AM -
Null Pointer Exception
By ScKaSx in forum New To JavaReplies: 1Last Post: 01-24-2009, 11:27 AM -
Null Pointer Exception
By Jacinth in forum New To JavaReplies: 4Last Post: 01-22-2009, 01:47 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks