Results 1 to 4 of 4
  1. #1
    bequick01 is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default 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]

  2. #2
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    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.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  4. #4
    snotmare is offline Member
    Join Date
    Apr 2011
    Posts
    34
    Rep Power
    0

    Default

    The problem is that you are declaring your members...
    Java Code:
    JTextField txInMortAmt = null;
    JTextField txInTerm = null;
    JTextField txInRate = null;
    ...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:
    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);
    ... 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");
    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

  1. null pointer exception
    By marvelk in forum Advanced Java
    Replies: 8
    Last Post: 02-01-2011, 09:02 AM
  2. Null pointer exception
    By izzy in forum New To Java
    Replies: 5
    Last Post: 03-22-2010, 05:19 PM
  3. Null Pointer Exception
    By andre1011 in forum Advanced Java
    Replies: 4
    Last Post: 02-07-2009, 03:30 AM
  4. Null Pointer Exception
    By ScKaSx in forum New To Java
    Replies: 1
    Last Post: 01-24-2009, 11:27 AM
  5. Null Pointer Exception
    By Jacinth in forum New To Java
    Replies: 4
    Last Post: 01-22-2009, 01:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •