Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 JTextField.getText() NullPointerException
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
randint




PostPosted: Wed Mar 13, 2013 10:28 pm   Post subject: JTextField.getText() NullPointerException

This code has a NullPointerException at the JTextField.getText() method:
Java:
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[] = new int [3], y[] = new int [3];
        private JTextField entries[] = new JTextField [6];
        private JTextField tResolution = new JTextField (50);
        public Shapes()
        {
                pApplet.add(polygon);
                pApplet.add(oval);
                add(pApplet);
                group.add(polygon);
                group.add(oval);
                polygon.addActionListener(this);
                oval.addActionListener(this);
                polygon_OK.addActionListener(this);
                fData.setSize(Toolkit.getDefaultToolkit().getScreenSize());
                fData.setContentPane(pData);
                fData.setResizable(false);
                fGraph.setSize(Toolkit.getDefaultToolkit().getScreenSize());
                fGraph.setContentPane(pGraph);
                fGraph.setResizable(false);
        }
        public void actionPerformed(ActionEvent e)
        {
                if (e.getSource() == polygon)
                {
                        boolean error = false;
                        while (!error)
                        {
                                try
                                {
                                        error = true;
                                        while (points < 3)
                                        {
                                                points = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the number of points", "Points", 1));
                                                if (points < 3)
                                                {
                                                        JOptionPane.showMessageDialog(null, "A polygon must have at least 3 points!", "Polygon Error", 0);
                                                }
                                        }
                                }
                                catch(Throwable t)
                                {
                                        error = false;
                                        JOptionPane.showMessageDialog(null, "The input is not a valid integer!", "Number Entry Error", 0);
                                }
                        }
                        x = new int [points];
                        y = new int [points];
                        entries = new JTextField [points * 2];
                        String s = "x";
                        for (int i = 0; i < points; i++)
                        {
                                for (int j = 0; j < 2; j++)
                                {
                                        if (j == 0)
                                        {
                                                s = "x";
                                        }
                                        else if (j == 1)
                                        {
                                                s = "y";
                                        }
                                        pData.add (new JLabel (s + " value " + (i + 1)));
                                        entries [i] = new JTextField (50);
                                        pData.add (entries [i]);
                                }
                        }
                        pData.add(new JLabel ("Resolution"));
                        pData.add(tResolution);
                        pData.add(polygon_OK);
                        fData.setVisible(true);
                }
                else if (e.getSource() == polygon_OK)
                {
                        int count = 0, j = 0, k = 0;
                        String string = "Sample string";
                        for (int i = 0; i < points * 2; i++)
                        {
                                string = entries [i].getText();
                                if (string == null)
                                {
                                        count++;
                                }
                        }
                        string = tResolution.getText();
                        if (string == null)
                        {
                                count++;
                        }
                        if (count == 0)
                        {
                                for (int i = 0; i < entries.length; i++)
                                {
                                        if (i % 2 == 0)
                                        {
                                                x [j] = Integer.parseInt (entries [i].getText());
                                                j++;
                                        }
                                        else if (i % 2 == 1)
                                        {
                                                y [k] = Integer.parseInt (entries [i].getText());
                                                k++;
                                        }
                                }
                                resolution = Integer.parseInt (tResolution.getText());
                        }
                }
        }
}

In other forums, I see that people are overriding global variables with local ones such as
Java:
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)

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).
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Wed Mar 13, 2013 10:52 pm   Post subject: Re: JTextField.getText() NullPointerException

Start reading the exception's stack trace
randint @ Wed Mar 13, 2013 10:28 pm wrote:

600Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at ShapesAssignment.actionPerformed(ShapesAssignment.java:96)


Presumably the line 96 is:
Quote:

for (int i = 0; i < points * 2; i++)
{
string = entries [i].getText();
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?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
randint




PostPosted: Thu Mar 14, 2013 8:45 am   Post subject: Re: JTextField.getText() NullPointerException

Thank you, Tony! I fixed it:
Java:

x = new int [points];
                        y = new int [points];
                        entries = new JTextField [points * 2];
                        String s = "x";
                        for (int i = 0; i < points * 2; i++)
                        {
                                if (i % 2 == 0)
                                {
                                        s = "x";
                                }
                                else if (i % 2 == 1)
                                {
                                        s = "y";
                                }
                                pData.add (new JLabel (s + " value " + (i + 1)));
                                entries [i] = new JTextField (50);
                                pData.add (entries [i]);
                        }

Yeah...I understand now, only half of the JTextField's are instantiated in my original set up......
md




PostPosted: Thu Mar 14, 2013 10:34 am   Post subject: 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




PostPosted: Thu Mar 14, 2013 8:59 pm   Post subject: 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




PostPosted: Thu Mar 14, 2013 9:10 pm   Post subject: Re: RE:JTextField.getText() NullPointerException

randint @ 2013-03-14, 8:59 pm wrote:
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.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: