0

so I got this problem with a GUI witch has to take some info from five JTextField's and put the info in an instance of a class libro (italian for book).

I have made some controlling if's to check if the string receved is not empty but I keep getting a NumberFormatExeption error in the terminal.

Code at the part the NumberFormatExeption happens

tTitle  = this.titleField.getText();
if(tTitle == null || tTitle == "")
    tTitle = "Titolo Non Conosciuto";
tAuthor  = this.authorField.getText();
if(tAuthor == null || tAuthor == "")
    tAuthor = "Autore Non Conosciuto";
tEditor = this.editorField.getText();
if(tEditor == null || tEditor == "")
    tEditor = "Editore Non Conosciuto";
tPages  = this.pagesField.getText();
if(tPages == null || tPages == "")
    tPages = "0";
tPrezzo  = this.priceField.getText();
if(tPrezzo == null || tPrezzo == "")
    tPrezzo = "0,0";
temp = new Libro(tTitle,tAuthor,tEditor,Integer.parseInt(tPages),Double.parseDouble(tPrezzo));

the terminal sais that the NumberFormatExeption is happening on the last statement in the code above. if needed here is the full error:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:592)
    at java.lang.Integer.parseInt(Integer.java:615)
    at MyPanel.actionPerformed(MyPanel.java:148)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6525)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6290)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4881)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2750)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

I don't even know exactly what NumberFormatExeption means. what does it mean and how can i make it not happen in the future?

BRHSM
  • 854
  • 3
  • 13
  • 48

3 Answers3

3

Seems like you try to parse an empty String because tPages = "0"; is never called.

Use tPages.isEmpty() in your if statement instead of tPages == ""

sinclair
  • 2,812
  • 4
  • 24
  • 53
0

When you are evaluating an object value, instead of a primitive format you will need to use operators from the object.

For String and other complex objects, you need to use internal methods to compare their internal values. you will need to use tPages.equals(""), tPages.isEmpty() and other values that you can see here instead of == "", you can see more details about this in this blog.

For your NumberFormatException, It's a common exception for Number object, when you create malformed numbers, once "" is not a number.

-2

Try 0.0 instead of 0,0. And see if it works.

SamuelD
  • 333
  • 2
  • 10