Results 1 to 4 of 4
- 10-22-2012, 12:08 AM #1
Member
- Join Date
- Oct 2012
- Location
- Fargo, ND
- Posts
- 1
- Rep Power
- 0
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();
}
});
}
}
- 10-22-2012, 12:41 AM #2
Member
- Join Date
- Sep 2012
- Posts
- 70
- Rep Power
- 0
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.
- 10-22-2012, 02:23 AM #3
Re: Text Field null pointer Exception
Hmmmm!Java Code:JTextField tf1; // elsewhere JTextField tf1 = new JTextField(50);
- 10-22-2012, 06:43 AM #4
Member
- Join Date
- Sep 2012
- Posts
- 70
- Rep Power
- 0
Similar Threads
-
Null pointer exception
By tnrh1 in forum New To JavaReplies: 1Last Post: 10-11-2012, 01:39 PM -
Null pointer exception a
By TaxpayersMoney in forum New To JavaReplies: 5Last Post: 08-16-2011, 12:37 AM -
Null Pointer Exception HELP!?
By 2wyked in forum New To JavaReplies: 3Last Post: 04-04-2011, 01:41 AM -
Null pointer exception
By samuel.roshni in forum Java ServletReplies: 14Last Post: 01-22-2011, 02:25 PM -
null pointer exception
By jyothi.priyanka in forum New To JavaReplies: 12Last Post: 03-11-2009, 05:04 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks