maxpesa
maxpesa

Reputation: 185

NumberFormatException when trying to convert JTextField to int

in this part of my code, i'm creating an object of the class Dizionarioand writing it to a file, first calling the construcor, accepting 3 parameters, (Path, String, int). I am getting these 3 parameters from 3 JTextField and particulary, the last one (JTextField3) is creating this error, converting to int

This is the error:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "javax.swing.JTextField[,62,11,302x28,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.synth.SynthBorder@9577f8b,flags=288,maximumSize=,minimumSize=,preferredSize=,caretColor=,disabledTextColor=DerivedColor(color=142,143,145 parent=nimbusDisabledText offsets=0.0,0.0,0.0,0 pColor=142,143,145,editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=DerivedColor(color=255,255,255 parent=nimbusSelectedText offsets=0.0,0.0,0.0,0 pColor=255,255,255,selectionColor=DerivedColor(color=57,105,138 parent=nimbusSelectionBackground offsets=0.0,0.0,0.0,0 pColor=57,105,138,columns=0,columnWidth=0,command=,horizontalAlignment=LEADING]"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.<init>(Integer.java:677)

I tried these pieces of code to convert the string to an integer:

int i = new Integer(jTextField3.toString()); 

and then putting i as parameter (or directly calling new Integer(...) as parameter)

(int)JTextField3.toString();

Integer.ParseInt(JTextField3.toString());

and here is my method

    private void CreateMouseClicked(java.awt.event.MouseEvent evt) {                                    
    Dizionario dic = new Dizionario(
            (Paths.get(jTextField2.toString())),
            jTextField1.toString(),
            Integer.parseInt(jTextField3.toString())); 
    dic.writeToFile();
}   

Upvotes: 1

Views: 1759

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347204

Don't use JTextField#toString, use JTextField#getText to return the text content of the text field, for example...

int i = new Integer(jTextField3.getText());

toString is generally used to provide useful diagnostic information about an Object

Upvotes: 2

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

um, it's not jTextField3.toString(), it's jTextField3.getText(). That's a big difference, and to see just what toString() returns, look at your error message. You're trying to parse this:

"javax.swing.JTextField[,62,11,302x28,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.synth.SynthBorder@9577f8b,flags=288,maximumSize=,minimumSize=,preferredSize=,caretColor=,disabledTextColor=DerivedColor(color=142,143,145 parent=nimbusDisabledText offsets=0.0,0.0,0.0,0 pColor=142,143,145,editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=DerivedColor(color=255,255,255 parent=nimbusSelectedText offsets=0.0,0.0,0.0,0 pColor=255,255,255,selectionColor=DerivedColor(color=57,105,138 parent=nimbusSelectionBackground offsets=0.0,0.0,0.0,0 pColor=57,105,138,columns=0,columnWidth=0,command=,horizontalAlignment=LEADING]"

Into a number.

Upvotes: 4

Related Questions