-2

I'm stuck on this bit of code I'm trying to do. I've got a program I'm working in Netbeans with a main java form and a JFrame that acts like a popup. The frame has a button which prints the value of what I've put in a Getter method in the main. The is no error in the code put once i run the program and click the button the program crashes and this is what i get:

"Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: 
empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122)"

This is my code for the main: `

    public void Iva(){

    float tiv = Float.parseFloat(tiva);     // tiva is a global, String type variable

    if("Papeleria".equals(tiva)){

        iva = (float) (tiv*0.16);      // iva is a global Float type variable 

    }

    if("Supermercado".equals(tiva)){
         iva = (float) (tiv*0.12);
    }

    if("Supermercado".equals(tiva)){

         iva = (float) (tiv*0.14);
    }
    ivi = Float.toString(iva);     // ivi is a global, String type variable
}

public String getIva() {
    Iva();
    return ivi;
}

And this is what Ive got for the ActionPerformed in the Frame:

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {

    /*taiva is the text area where i want to show iva
    mainy is what i called the main's class*/

    taiva.setText(mainy.getIva()); 
}    
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    Not your down-voter (yet) but this whole set-up sounds sketchy, but it's hard to say since you've only partially described your program structure and have only posted snippets. For us to much more completely understand your problem, do consider creating and posting a ***small*** but functioning program that we can compile and run and that shows the problem for us, a valid [mcve], and posting the code in your question as code-formatted text. – Hovercraft Full Of Eels Mar 18 '18 at 17:12
  • 1
    Also understand that your use of a JFrame as a dialog window is not correct, that this should instead be a JDialog. Please read [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636) for more on this. – Hovercraft Full Of Eels Mar 18 '18 at 17:12
  • Thank you so much for replying, I will work on putting the frame's contents in a jDialog and will update my question to a Minimal, Complete, and Verifiable example once I'm done. – Daniela Villamizar Mendoza Mar 18 '18 at 17:41
  • 1
    Thank you. Also note that Java doesn't have "global" variables, and if you mean that you're using static fields to allow multiple classes to share the data -- you shouldn't do this. – Hovercraft Full Of Eels Mar 18 '18 at 17:43
  • Hovercraft Full of Eels I've changed the frame and put it into a JDialog but it wont show when I press the button but only the top bar shows up I've already adjusted the minimum size – Daniela Villamizar Mendoza Mar 19 '18 at 00:36
  • 1
    So again we're stuck with something being wrong in code not shown. Again a decent [mcve] would help wondrously. – Hovercraft Full Of Eels Mar 19 '18 at 01:55

1 Answers1

1

Your program is failing on this line of code:

float tiv = Float.parseFloat(tiva);     // tiva is a global, String type variable

You are trying to parse an empty string (tiva) which results in an NumberFormatException.

You can avoid this problem by using an if-statement, like this:

float tiv = 0;
if (!tiva.isEmpty()) {
  tiv = Float.parseFloat(tiva);
} else {
  tiv = 0;
}

Hope this helps ;)

Tostifrosti
  • 143
  • 9