You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@netbeans.apache.org by Zulfi Khan <zu...@yahoo.com.INVALID> on 2024/08/03 18:56:41 UTC
NumberFormatException
Hi,
I have written thefollowing function:
private booleanjDelBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO addyour handling code here:
JOptionPane.showMessageDialog(null, "Inside Delete Btn");
String str ="";
try{
str =jTF1.getText().trim();
}catch(java.lang.NumberFormatException e){
JOptionPane.showMessageDialog(null, "Inside Exception Handler");
}
int len =str.length();
JOptionPane.showMessageDialog(null, "len="+ len+"str="+str);
if(str ==""){//WHY THIS LINE NOT EXECUTED
JOptionPane.showMessageDialog(null, "No Data to Delete");
returnfalse;
}
System.out.println("TESTING TESTING TESTING");
int data =Integer.parseInt(jTF1.getText().trim());//NUMBER FORMAT Exception
//Integer.parseInt(quantityString.trim())
System.out.println("data = "+data);
obj.delete(data);
return true;
}
If my TextField1(i.e. TF1) is empty I am getting Number Format Exception. I havecopied exception generating function. The exception is:
Exception in thread"AWT-EventQueue-0" java.lang.NumberFormatException: Forinput string: ""
atjava.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
atjava.base/java.lang.Integer.parseInt(Integer.java:678)
atjava.base/java.lang.Integer.parseInt(Integer.java:786)
atcom.mycompany.linkedlistdemo.LLJFrame.jDelBtnActionPerformed(LLJFrame.java:158)
atcom.mycompany.linkedlistdemo.LLJFrame$5.actionPerformed(LLJFrame.java:75)
atjava.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
atjava.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
atjava.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
atjava.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
atjava.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
atjava.desktop/java.awt.Component.processMouseEvent(Component.java:6626)
atjava.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3389)
atjava.desktop/java.awt.Component.processEvent(Component.java:6391)
atjava.desktop/java.awt.Container.processEvent(Container.java:2266)
atjava.desktop/java.awt.Component.dispatchEventImpl(Component.java:5001)
atjava.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
atjava.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
atjava.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)
atjava.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575)
atjava.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)
atjava.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)
atjava.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
atjava.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
atjava.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
atjava.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
atjava.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
atjava.base/java.security.AccessController.doPrivileged(AccessController.java:399)
atjava.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
atjava.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
atjava.desktop/java.awt.EventQueue$5.run(EventQueue.java:746)
atjava.desktop/java.awt.EventQueue$5.run(EventQueue.java:744)
atjava.base/java.security.AccessController.doPrivileged(AccessController.java:399)
atjava.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
atjava.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:743)
atjava.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
atjava.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
atjava.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
atjava.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
atjava.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
atjava.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
I have also attachedcomplete project.
Somebody pleaseguide me.
Zulfi.
------------------------------------------------------------------------
Re: NumberFormatException
Posted by Scott Palmer <sw...@gmail.com>.
Change
if (str ==“”)
to
if (str.isEmpty())
Don’t use == to compare strings. The empty string from the text field is a
different entity than the constant pool empty string. You are doing an
identity comparison.
Scott
> On Aug 3, 2024, at 10:17 PM, Pieter van den Hombergh
> <pi...@gmail.com> wrote:
>
>
>
>
> an empty string is never a valid number. for a valid number you need at
> least one digit inside the string.
>
> met vriendelijke groet
> Pieter van den Hombergh
>
>
>
>
> Op za 3 aug 2024, 20:58 schreef Zulfi Khan <zu...@yahoo.com.invalid>:
>
>
>> Hi,
>>
>>
>
>>
>> I have written the following function:
>>
>>
>
>>
>> private boolean jDelBtnActionPerformed(java.awt.event.ActionEvent evt) {
>>
>> // TODO add your handling code here:
>>
>> JOptionPane.showMessageDialog(null, "Inside Delete Btn");
>>
>> String str = "";
>>
>> try{
>>
>> str = jTF1.getText().trim();
>>
>> }catch(java.lang.NumberFormatException e){
>>
>> JOptionPane.showMessageDialog(null, "Inside Exception Handler");
>>
>> }
>>
>> int len = str.length();
>>
>> JOptionPane.showMessageDialog(null, "len="+ len+"str=" +str);
>>
>> if(str == ""){//WHY THIS LINE NOT EXECUTED
>>
>> JOptionPane.showMessageDialog(null, "No Data to Delete");
>>
>> return false;
>>
>> }
>>
>> System.out.println("TESTING TESTING TESTING");
>>
>> int data = Integer.parseInt(jTF1.getText().trim());//NUMBER FORMAT
Exception
>>
>> //Integer.parseInt(quantityString.trim())
>>
>> System.out.println("data = "+data);
>>
>> obj.delete(data);
>>
>> return true;
>>
>> }
>>
>>
>
>>
>> If my TextField1 (i.e. TF1) is empty I am getting Number Format Exception.
I have copied exception generating function. The exception is:
>>
>>
>
>>
>>
>
>>
>>
>
>>
>> Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For
input string: ""
>>
>> at
java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
>>
>> at java.base/java.lang.Integer.parseInt(Integer.java:678)
>>
>> at java.base/java.lang.Integer.parseInt(Integer.java:786)
>>
>> at
com.mycompany.linkedlistdemo.LLJFrame.jDelBtnActionPerformed(LLJFrame.java:158)
>>
>> at
com.mycompany.linkedlistdemo.LLJFrame$5.actionPerformed(LLJFrame.java:75)
>>
>> at
java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
>>
>> at
java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
>>
>> at
java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
>>
>> at
java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
>>
>> at
java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
>>
>> at java.desktop/java.awt.Component.processMouseEvent(Component.java:6626)
>>
>> at
java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3389)
>>
>> at java.desktop/java.awt.Component.processEvent(Component.java:6391)
>>
>> at java.desktop/java.awt.Container.processEvent(Container.java:2266)
>>
>> at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5001)
>>
>> at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
>>
>> at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
>>
>> at
java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)
>>
>> at
java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575)
>>
>> at
java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)
>>
>> at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)
>>
>> at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
>>
>> at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
>>
>> at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
>>
>> at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
>>
>> at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
>>
>> at
java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
>>
>> at
java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
>>
>> at
java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
>>
>> at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:746)
>>
>> at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:744)
>>
>> at
java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
>>
>> at
java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
>>
>> at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:743)
>>
>> at
java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
>>
>> at
java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
>>
>> at
java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
>>
>> at
java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
>>
>> at
java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
>>
>> at
java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
>>
>>
>
>>
>> I have also attached complete project.
>>
>>
>
>>
>> Somebody please guide me.
>>
>>
>
>>
>> Zulfi.
>>
>> \------------------------------------------------------------------------
>>
>>
>
>>
>>
> \---------------------------------------------------------------------
> To unsubscribe, e-mail: [users-
> unsubscribe@netbeans.apache.org](mailto:users-
> unsubscribe@netbeans.apache.org)
> For additional commands, e-mail: [users-
> help@netbeans.apache.org](mailto:users-help@netbeans.apache.org)
>
> For further information about the NetBeans mailing lists, visit:
> <https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists>
Re: NumberFormatException
Posted by Pieter van den Hombergh <pi...@gmail.com>.
an empty string is never a valid number. for a valid number you need at
least one digit inside the string.
met vriendelijke groet
Pieter van den Hombergh
Op za 3 aug 2024, 20:58 schreef Zulfi Khan <zu...@yahoo.com.invalid>:
> Hi,
>
> I have written the following function:
>
>
> private boolean jDelBtnActionPerformed(java.awt.event.ActionEvent evt) {
>
> // TODO add your handling code here:
>
> JOptionPane.showMessageDialog(null, "Inside Delete Btn");
>
> String str = "";
>
> try{
>
> str = jTF1.getText().trim();
>
> }catch(java.lang.NumberFormatException e){
>
> JOptionPane.showMessageDialog(null, "Inside Exception Handler");
>
> }
>
> int len = str.length();
>
> JOptionPane.showMessageDialog(null, "len="+ len+"str=" +str);
>
> if(str == ""){//WHY THIS LINE NOT EXECUTED
>
> JOptionPane.showMessageDialog(null, "No Data to Delete");
>
> return false;
>
> }
>
> System.out.println("TESTING TESTING TESTING");
>
> int data = Integer.parseInt(jTF1.getText().trim());//NUMBER FORMAT
> Exception
>
> //Integer.parseInt(quantityString.trim())
>
> System.out.println("data = "+data);
>
> obj.delete(data);
>
> return true;
>
> }
>
>
> If my TextField1 (i.e. TF1) is empty I am getting Number Format Exception.
> I have copied exception generating function. The exception is:
>
>
>
>
> Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException:
> For input string: ""
>
> at
> java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
>
> at java.base/java.lang.Integer.parseInt(Integer.java:678)
>
> at java.base/java.lang.Integer.parseInt(Integer.java:786)
>
> at
> com.mycompany.linkedlistdemo.LLJFrame.jDelBtnActionPerformed(LLJFrame.java:158)
>
> at
> com.mycompany.linkedlistdemo.LLJFrame$5.actionPerformed(LLJFrame.java:75)
>
> at
> java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
>
> at
> java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
>
> at
> java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
>
> at
> java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
>
> at
> java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
>
> at java.desktop/java.awt.Component.processMouseEvent(Component.java:6626)
>
> at
> java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3389)
>
> at java.desktop/java.awt.Component.processEvent(Component.java:6391)
>
> at java.desktop/java.awt.Container.processEvent(Container.java:2266)
>
> at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5001)
>
> at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
>
> at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
>
> at
> java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)
>
> at
> java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575)
>
> at
> java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)
>
> at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)
>
> at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
>
> at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
>
> at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
>
> at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
>
> at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
>
> at
> java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
>
> at
> java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
>
> at
> java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
>
> at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:746)
>
> at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:744)
>
> at
> java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
>
> at
> java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
>
> at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:743)
>
> at
> java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
>
> at
> java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
>
> at
> java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
>
> at
> java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
>
> at
> java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
>
> at
> java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
>
>
> I have also attached complete project.
>
>
> Somebody please guide me.
>
>
> Zulfi.
>
> ------------------------------------------------------------------------
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@netbeans.apache.org
> For additional commands, e-mail: users-help@netbeans.apache.org
>
> For further information about the NetBeans mailing lists, visit:
> https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists