How do I solve an Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String?

3

My code is this:

import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*; 






/*
 *- Interfaz Nomina -
 *  Programa que muestra una interfaz para consultar nominas.

 */

/**
 *
 * @author Marily Huerta/Frida Fuentes.
 * @since 03/diciembre/2018
 */
public class InterfazNominaSueldo extends JFrame implements ActionListener{
      JLabel T1,T2,T3;
      JLabel Empleado,HorasTrabajadas,RN,Sue,PHEX,SueB,Dedu,DIMSS,DISPT,SueN;
      JButton BCerrar,BLimpiar,BSueldoN;
      JTextField NomEmp,HT,S,PEXT,SB,DEDU,DEDUIMSS,DEDUISPT,SN;

      double SUELDONETO,HRST,SUELDO,PAGOEXT,SUEB,DEDUC,DEDIMSS,DEDISPT,SUEN;
    private double PAGOEXTRA;
      public InterfazNominaSueldo(){
         setLayout(null);

            getContentPane().setBackground(Color.red);
          T1=new JLabel("   C.B.T.I.S  No. 189");
             T1.setBounds(120,10,120,50);    
             add(T1);
          T2= new JLabel("  CALCULAR NOMINA EMPLEADOS");
          T2.setBounds(100,30,300,50);    
             add(T2);

          T3= new JLabel("     INTRODUCIR DATOS");
          T3.setBounds(90,50,500,50);    
             add(T3);


        Empleado=new JLabel("  Empleado");
        Empleado.setBounds(30,90,700,50);    
             add(Empleado);


        HorasTrabajadas= new JLabel(" Horas Trabajadas ");
        HorasTrabajadas.setBounds(30,120,900,50);    
             add(HorasTrabajadas);

        RN=new JLabel("Recibo Nomina");  
        RN.setBounds(140, 215, 900, 50);
           add(RN);  
        Sue=new JLabel(" Sueldo");     
        Sue.setBounds(90, 255, 900, 50);
        add(Sue);
        PHEX= new JLabel("Pago Horas Extra");
        PHEX.setBounds(30, 280, 900, 50);
        add(PHEX);
        SueB=new JLabel("Sueldo Bruto");
        SueB.setBounds(55,300,900,50);
        add(SueB);
        Dedu=new JLabel("Deducciones");
        Dedu.setBounds(50,320,900,50);
        add(Dedu);
        DIMSS=new JLabel("IMSS");
        DIMSS.setBounds(97,340,900,50);
        add(DIMSS);
        DISPT=new JLabel("ISPT");
        DISPT.setBounds(98,360,900,50);
        add(DISPT);
        SueN=new JLabel("Sueldo Neto");
        SueN.setBounds(58,395,900,50);
        add(SueN);

        BCerrar=new JButton(" CERRAR");
        BCerrar.setBounds(10,200 ,90 ,20 );
        add(BCerrar);
        BCerrar.addActionListener(this);


    BLimpiar=new JButton(" LIMPIAR ");
        BLimpiar.setBounds(120,200 ,100 ,20 );
        add(BLimpiar);
        BLimpiar.addActionListener(this);


    BSueldoN=new JButton(" SUELDO ");
        BSueldoN.setBounds(250,200 ,100 ,20 );
        add(BSueldoN);
        BSueldoN.addActionListener(this);    

     NomEmp=new JTextField();   
      NomEmp.setBounds(160,110,100,20);
            add(NomEmp);
HT=new JTextField();   
      HT.setBounds(160,135,90,20);
            add(HT);


 S=new JTextField();
    S.setBounds(150,268 ,100 ,20 );
        add(S);

 PEXT=new JTextField();
 PEXT.setBounds(150,290,100,20);
        add(PEXT);
 SB=new JTextField();
 SB.setBounds(150,312,100,20);
       add(SB);
 DEDU=new JTextField();
 DEDU.setBounds(150,335,100,20);
       add(DEDU);

 DEDUIMSS=new JTextField();
 DEDUIMSS.setBounds(150,358,100,20);
       add(DEDUIMSS);

  DEDUISPT=new JTextField();
 DEDUISPT.setBounds(150,380,100,20);
       add(DEDUISPT);  
   SN=new JTextField();
 SN.setBounds(150,410,100,20);
       add(SN);  
      } 





    public static void main(String[] args) {
         InterfazNominaSueldo Ventana=new InterfazNominaSueldo();
          Ventana.setBounds(0,0,400,500);
          Ventana.setVisible(true);
          Ventana.setLocationRelativeTo(null);
    }



public void actionPerformed(Action EventEvento){
        if (Evento.getSource() == BCerrar){
           System.exit(0);
       }

       if (Evento.getSource() == BLimpiar){
           NomEmp.setText("");
           HT.setText("");
           S.setText("");
           PEXT.setText("");
           SB.setText("");
           DEDU.setText("");
           DEDUIMSS.setText("");
           SN.setText("");
       }

       if (Evento.getSource() == BSueldoN){

    }
    }
      @Override

    public void actionPerformed(ActionEvent Evento) {
        if (Evento.getSource() == BCerrar){
           System.exit(0);
       }

       if (Evento.getSource() == BLimpiar){
           NomEmp.setText("");
           HT.setText("");
           S.setText("");
           PEXT.setText("");
           SB.setText("");
           DEDU.setText("");
           DEDUIMSS.setText("");
           SN.setText("");
       }

       if (Evento.getSource() == BSueldoN){
    HRST=Float.parseFloat(HT.getText());
    PAGOEXTRA=Float.parseFloat(PEXT.getText()); 
    final float SUEHRS=70;
    final float SUEHX=140;
    double HRSEXT;

    if(HRST>40)
    {

        HRSEXT=HRST-40;
        PAGOEXTRA= HRSEXT*SUEHX;
        HRST=40;
    }
    SUELDO=(HRST*SUEHRS);
    SUEB=SUELDO+PAGOEXT;
    DEDIMSS=(float) (SUEB*0.03);
    DEDISPT=(float) (SUEB*0.02);
       DEDUC=DEDIMSS+DEDISPT;
       SUEN=SUEB-DEDIMSS-DEDISPT;


       S.setText(String.valueOf(SUELDO));
       SB.setText(String.valueOf(SUEB));
       DEDU.setText(String.valueOf(DEDUC));
       DEDUIMSS.setText(String.valueOf(DEDIMSS));
       DEDUISPT.setText(String.valueOf(DEDISPT));
       SN.setText(String.valueOf(SUEN)); 




    }
       }
    }
    
asked by Marily Huerta 05.12.2018 в 22:58
source

2 answers

0

The problem is generated here:

HRST=Float.parseFloat(HT.getText());
PAGOEXTRA=Float.parseFloat(PEXT.getText()); 

Remember that to perform the conversion you must do it from numeric values, if you enter a non-numeric value such as a letter, a space or an empty string in HT and PEXT you will get the error:

  

NumberFormatException: empty String?

in this case it is indicating that the JTextField do not really have a specified value.

For this you can perform a validation using the method:

public float validaFloat(String number){
    float result = 0; //Valor default.
    try{
        if(number != null){
            result = Float.parseFloat(number);
        }
    }catch(NumberFormatException nfe){
        //*No es numerico!
    }
    return result;
}

and you would use it this way:

//HRST=Float.parseFloat(HT.getText());
  HRST=validaFloat(HT.getText());
//PAGOEXTRA=Float.parseFloat(PEXT.getText()); 
  PAGOEXTRA=validaFloat(PEXT.getText()); 
    
answered by 06.12.2018 в 00:33
0

The problem actually results from trying to parse the lines:

HRST=Float.parseFloat(HT.getText());
PAGOEXTRA=Float.parseFloat(PEXT.getText());

For this type of interface I personally apply a restriction to the JTextField to force the use of numbers and a period, like this:

    HT.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyTyped(java.awt.event.KeyEvent evt) {
            char caracter = evt.getKeyChar();
            //Verificar si ya existe un punto
            if (jPrecio.getText().contains(".") && (caracter == '.')) {
                evt.consume();
            }
            // Verificar si la tecla pulsada no es un digito
            if (((caracter < '0')
                    || (caracter > '9'))
                    && (caracter != '.')
                    && (caracter != '\b' /*corresponde a BACK_SPACE*/)) {
                evt.consume();  // ignorar el evento de teclado
            }
        }
    });

And, just recognized the event, verify the existence of the data or request it like this:

  if (Evento.getSource() == BSueldoN){
    if (HT.getText().equals("")) {
        JOptionPane.showConfirmDialog(null,
                "Ingrese las horas trabajadas",
                "Información faltante",
                JOptionPane.DEFAULT_OPTION);
        return;
    } else {
        HRST = Float.parseFloat(HT.getText());
    }
....

Confirmed the data is proceeded to parsear safely. This way the application does not stop and you could not continue with the calculations without the necessary information.

P.D. It would have to be implemented also for the JTextField "PEXT"

Greetings

    
answered by 02.01.2019 в 13:05