Search code examples
javaswinginputjtextfieldnumberformatexception

NumberFormatException on parsing JTextField input


The Java applet has a working code, I think. However, I am not getting my program executed the way I want it to.

Here is the message it shows when it runs (on parsing JTextField input):

java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at skiRace.init(skiRace.java:24)
    at ready.AppletRunner.run(AppletRunner.java:209)
    at java.lang.Thread.run(Unknown Source)

Here is my code:

import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class skiRace extends Applet implements ActionListener
{
int txt = 0;
int txt2 = 0;
int txt3 = 0;
int txt4 = 0;

public void init ()
{
    resize (300, 350);
    setBackground (Color.blue);
    JLabel pic = new JLabel (createImageIcon ("race.jpg"));

    JLabel ins = new JLabel ("Enter the number of points beside each skier.");
    ins.setFont (new Font ("Arial", Font.BOLD, 12));

    JLabel p1 = new JLabel (createImageIcon ("player1.jpg"));
    JTextField answer = new JTextField (3);
    int txt = Integer.parseInt (answer.getText ());

    JLabel p2 = new JLabel (createImageIcon ("player2.jpg"));
    JTextField answer2 = new JTextField (3);
    int txt2 = Integer.parseInt (answer2.getText ());

    JLabel p3 = new JLabel (createImageIcon ("player3.jpg"));
    JTextField answer3 = new JTextField (3);
    int txt3 = Integer.parseInt (answer3.getText ());

    JLabel p4 = new JLabel (createImageIcon ("player4.jpg"));
    JTextField answer4 = new JTextField (3);
    int txt4 = Integer.parseInt (answer4.getText ());

    JButton done = new JButton ("Done");
    done.setActionCommand ("done");
    done.addActionListener (this);

    add (pic);
    add (ins);
    add (p1);
    add (answer);
    add (answer2);
    add (answer3);
    add (answer4);
    add (done);

}


public void actionPerformed (ActionEvent e)
{
    if (e.getActionCommand ().equals ("done"))
    {
        if ((txt == 7) && (txt2 == 8) && (txt3 == 6) && (txt4 == 9))
        {
            JOptionPane.showMessageDialog (null, "You got it! Great work!", "Correct",
                    JOptionPane.INFORMATION_MESSAGE);
        }
        else
        {
            JOptionPane.showMessageDialog (null, "You are incorrect, please try again.",
                    "Incorrect", JOptionPane.ERROR_MESSAGE);
        }
    }
}


protected static ImageIcon createImageIcon (String path)
{
    java.net.URL imgURL = dice.class.getResource (path);
    if (imgURL != null)
    {
        return new ImageIcon (imgURL);
    }
    else
    {
        System.err.println ("Couldn't find file: " + path);
        return null;
    }
}
}

Why is this happening?

I think it has something to do with the dialog boxes or text fields with input, since this is my first program using either of those two functions in a Java applet.


Solution

  • Since this line (# 24) is in the init() method:

    int txt = Integer.parseInt (answer.getText ());
    

    answer will have the exact same text it was constructed with (i.e. nothing - an empty string). An empty string is not an integer or parsable to an integer.


    I would recommend instead:

    • Create a SpinnerNumberModel and keep a reference to that model. Add the model in the constructor of a JSpinner instead of the JTextField.
    • Act on the number (obtained by querying the model) either when the value of the spinner changes, or when the user activates a button, rather than directly after it was created and before the user ever sees the control, let alone gets to change it.

    Other notes:

    • Don't 'shadow' variables. E.G.

      int txt4 = 0;
      

      is declared as a class attribute, but it is declared again as a local variable within the init() method. E.G.

      int txt4 = Integer.parseInt (answer4.getText ());
      

      To fix it, change that to:

      txt4 = Integer.parseInt (answer4.getText ()); // use class attribute to store result