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). |