
-----------------------------------
randint
Wed Mar 13, 2013 10:28 pm

JTextField.getText() NullPointerException
-----------------------------------
This code has a NullPointerException at the JTextField.getText() method:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Shapes extends JApplet implements ActionListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 7884507709512206814L;
	private JFrame fData = new JFrame ("Data Entry Point");
	private JFrame fGraph = new JFrame ("Shape Display");
	private JPanel pData = new JPanel ();
	private JPanel pGraph = new JPanel ();
	private JPanel pApplet = new JPanel ();
	private JRadioButton polygon = new JRadioButton ("Polygon");
	private JRadioButton oval = new JRadioButton ("Oval");
	private JButton polygon_OK = new JButton ("OK");
	private ButtonGroup group = new ButtonGroup ();
	private int points = 0, resolution = 0, x
In other forums, I see that people are overriding global variables with local ones such as
import javax.swing.*;
public class Random
{
   public static JFrame frame = new JFrame ("Frame");
   //...
   public void actionPerformed(ActionEvent e)
   {
      JFrame frame = new JFrame ("Frame 2");
      //The local frame will be used
   }

The exception is like this:
[code]200
600Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
	at ShapesAssignment.actionPerformed(ShapesAssignment.java:96)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.plaf.basic.BasicButtonListener$Actions.actionPerformed(Unknown Source)
	at javax.swing.SwingUtilities.notifyAction(Unknown Source)
	at javax.swing.JComponent.processKeyBinding(Unknown Source)
	at javax.swing.JComponent.processKeyBindings(Unknown Source)
	at javax.swing.JComponent.processKeyEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
	at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)

600
200
	at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
	at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
	at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$000(Unknown Source)
	at java.awt.EventQueue$1.run(Unknown Source)
	at java.awt.EventQueue$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue$2.run(Unknown Source)
	at java.awt.EventQueue$2.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
[/code]
Please help! I do not understand this at all! I have dealt with JTextField's before (a lot of times, but not an array of JTextField's or grabbing int's from them).

-----------------------------------
Tony
Wed Mar 13, 2013 10:52 pm

Re: JTextField.getText() NullPointerException
-----------------------------------
Start reading the exception's stack trace

600Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
	at ShapesAssignment.actionPerformed(ShapesAssignment.java:96)

Presumably the line 96 is:

for (int i = 0; i < points * 2; i++) 
                        { 
                                string = entries 
                                if (string == null) 
                                { 
                                        count++; 
                                } 
                        } 


Which suggests that some entry is actually null and thus doesn't have the #getText() method available.

Are you sure that you have points*2 entries and that all of them are not null?

-----------------------------------
randint
Thu Mar 14, 2013 8:45 am

Re: JTextField.getText() NullPointerException
-----------------------------------
Thank you, Tony! I fixed it:

x = new int 
Yeah...I understand now, only half of the JTextField's are instantiated in my original set up......

-----------------------------------
md
Thu Mar 14, 2013 10:34 am

RE:JTextField.getText() NullPointerException
-----------------------------------
(not only directed at this thread)

It's amazing how easy it is to correct errors if you read stack traces and compiler errors. It's almost like they were written to tell you exactly what went wrong and where it happened!

-----------------------------------
randint
Thu Mar 14, 2013 8:59 pm

RE:JTextField.getText() NullPointerException
-----------------------------------
Yeah...I understand, but the exception is very implicit, I do not know where exactly I did wrong, you know.

-----------------------------------
md
Thu Mar 14, 2013 9:10 pm

Re: RE:JTextField.getText() NullPointerException
-----------------------------------
Yeah...I understand, but the exception is very implicit, I do not know where exactly I did wrong, you know.

It gave you the error, the file name, and the line number. From there it should be easy to work backwards and find the source of the error. Using a debugger and stepping through your code would probably also help tremendously.
