Results 1 to 4 of 4
Like Tree2Likes
  • 2 Post By Junky

Thread: Text Field null pointer Exception

  1. #1
    jakedebus is offline Member
    Join Date
    Oct 2012
    Location
    Fargo, ND
    Posts
    1
    Rep Power
    0

    Default Text Field null pointer Exception

    When I examine my code, I see no issues with it, however, when I run the code, the terminal window displays a null pointer exception on line 95, which is the line that says, tf1.getText() . The error message is as follows: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException. My code is pasted below. Any and all input as to how I might fix this would be GREATLY appreciated.



    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;

    public class CircleTest extends JFrame
    {
    JLabel label1, label2, label3, label4, answerLabel1, answerLabel2;
    JButton button;
    JTextField tf1;
    JFrame frame;
    JPanel descriptionPanel, promptPanel, answerPanel, mainpanel, topPanel;
    JMenuBar menubar, menu;
    JMenu file;
    JMenuItem clear;

    public CircleTest()
    {
    guiComponents();
    }

    public void guiComponents()
    {
    JLabel label1 = new JLabel("This program is designed to calculate the Area and Perimeter provided the radius");
    JLabel label2 = new JLabel("Enter Radius:");
    JLabel label3 = new JLabel("Area of Circle:");
    JLabel label4 = new JLabel("Perimeter of Circle:");
    JLabel answerLabel1 = new JLabel("");
    JLabel answerLabel2 = new JLabel("");

    menubar = new JMenuBar();
    setJMenuBar(menubar);

    file = new JMenu("File");
    menubar.add(file);

    clear = new JMenuItem("Clear");
    file.add(clear);

    JButton button = new JButton("Submit");

    JTextField tf1 = new JTextField(50);

    JPanel mainpanel = new JPanel(new BorderLayout(5,5));
    mainpanel.setBorder(new EmptyBorder(4,4,4,4));
    mainpanel.setSize(500,500);

    JFrame frame = new JFrame("Circle Calculator");
    frame.setContentPane(mainpanel);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.setSize(600,400);

    JPanel descriptionPanel = new JPanel(new FlowLayout());
    descriptionPanel.setBorder(new EmptyBorder(4,4,4,4));
    descriptionPanel.add(label1);

    JPanel promptPanel = new JPanel(new BorderLayout(2,2));
    promptPanel.setBorder(new EmptyBorder(4,4,4,4));
    promptPanel.add(label2, BorderLayout.NORTH);
    promptPanel.add(tf1, BorderLayout.WEST);
    promptPanel.add(button, BorderLayout.EAST);

    JPanel answerPanel = new JPanel(new GridLayout(2, 5, 2, 2));
    answerPanel.setBackground(Color.white);
    answerPanel.setBorder(BorderFactory.createLineBord er(Color.black));
    answerPanel.add(label3);
    answerPanel.add(answerLabel1);
    answerPanel.add(label4);
    answerPanel.add(answerLabel2);

    event1 ev1 = new event1();
    button.addActionListener(ev1);

    frame.add(answerPanel, BorderLayout.NORTH);
    frame.add(promptPanel, BorderLayout.CENTER);
    frame.add(descriptionPanel, BorderLayout.SOUTH);
    frame.setSize(500,500);
    frame.setJMenuBar(menubar);
    frame.pack();
    frame.setVisible(true);
    }

    public class event1 implements ActionListener
    {
    public void actionPerformed(ActionEvent ev1)
    {
    String radiusString = new String(tf1.getText());
    double rad = Double.parseDouble(radiusString);

    Circle calculate = new Circle();
    calculate.Area(rad);
    calculate.Perimeter(rad);

    double areaD = calculate.getArea();
    double circ = calculate.getPerimeter();

    String area = Double.toString(areaD);
    String perimeter = Double.toString(circ);

    answerLabel1.setText(area);
    answerLabel2.setText(perimeter);
    }
    }

    public static void main(String args [])
    {
    java.awt.EventQueue.invokeLater(new Runnable()
    {

    @Override
    public void run()
    {
    new CircleTest();
    }
    });
    }
    }

  2. #2
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default Re: Text Field null pointer Exception

    Its hard to say with only some of your code posted. I can't run it from my self. I haven't used action listeners much but I believe you usually use a switch statement in that actionPerformed() method to determent when a given action has happened.

    My guess is that this code runs everything in actionPerformed regarless of if you did said action or not. Therefor like your error says when it trys to tf1.getText() there is no text in that JTextField.

    I could be wrong though. Like i said I haven't used Listeners much.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Text Field null pointer Exception

    Java Code:
    JTextField tf1;
    // elsewhere
    JTextField tf1 = new JTextField(50);
    Hmmmm!
    DarrylBurke and killutch like this.

  4. #4
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default Re: Text Field null pointer Exception

    Quote Originally Posted by Junky View Post
    Java Code:
    JTextField tf1;
    // elsewhere
    JTextField tf1 = new JTextField(50);
    Hmmmm!
    nice catch. That's good game.

Similar Threads

  1. Null pointer exception
    By tnrh1 in forum New To Java
    Replies: 1
    Last Post: 10-11-2012, 01:39 PM
  2. Null pointer exception a
    By TaxpayersMoney in forum New To Java
    Replies: 5
    Last Post: 08-16-2011, 12:37 AM
  3. Null Pointer Exception HELP!?
    By 2wyked in forum New To Java
    Replies: 3
    Last Post: 04-04-2011, 01:41 AM
  4. Null pointer exception
    By samuel.roshni in forum Java Servlet
    Replies: 14
    Last Post: 01-22-2011, 02:25 PM
  5. null pointer exception
    By jyothi.priyanka in forum New To Java
    Replies: 12
    Last Post: 03-11-2009, 05:04 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
  •