Unparseable date: JAVA SQL

3

Checking that all the data are filled in in the form to be able to continue with the application, I find the following error:

ene 08, 2017 9:14:27 PM interfaz.Alta_vehiculo button_OKActionPerformed
GRAVE: null
java.text.ParseException: Unparseable date: ""
    at java.text.DateFormat.parse(DateFormat.java:366)
    at interfaz.Alta_vehiculo.button_OKActionPerformed(Alta_vehiculo.java:446)
    at interfaz.Alta_vehiculo.access$200(Alta_vehiculo.java:25)
    at interfaz.Alta_vehiculo$3.actionPerformed(Alta_vehiculo.java:194)

Code:

private void button_OKActionPerformed(java.awt.event.ActionEvent evt) {                                          
        //Atributos generales para todo tipo de vehículos.
        String bastidor = textfield_bastidor.getText();
        String color = (String) combobox_color.getSelectedItem();
        String matricula = textfield_matricula.getText();
        int marca = combobox_marcas.getSelectedIndex()+1;
        String modelo = (String) combobox_modelos.getSelectedItem();
        String descripcion = textfield_descripcion.getText();

        //Excepción por si el valor introducido no es un entero.
        int potencia = 0;
        try{
            potencia = Integer.parseInt(textfield_potencia.getText());
        }catch(NumberFormatException nfe_potencia){
            nfe_potencia.getMessage();
        }
        //Excepción por si el valor introducido no es un double.
        double consumo = 0.0;
        try{
            consumo = Double.parseDouble(textfield_consumo.getText());
        }catch(NumberFormatException nfe_consumo){
            nfe_consumo.getMessage();
        }
        String fecha_fabricacion = textfield_fecha_fabricacion.getText();
        //Damos formato a la fecha a insertar. Por ejemplo: 2010/10/10.
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        Date f = null;
        try {
            //Convertimos a Date la fecha recogida como String.
            f = sdf.parse(fecha_fabricacion);
        } catch (ParseException ex) {
            Logger.getLogger(Alta_vehiculo.class.getName()).log(Level.SEVERE, null, ex);
        }

        //Comprobamos que estan todos los datos insertados...
        if(!textfield_bastidor.getText().isEmpty() && !textfield_consumo.getText().isEmpty() && 
           !textfield_descripcion.getText().isEmpty() && !textfield_fecha_fabricacion.getText().isEmpty() && 
           !textfield_matricula.getText().isEmpty() && !textfield_potencia.getText().isEmpty() && 
           !textfield_precio.getText().isEmpty() && !textfield_precio.getText().isEmpty()){
            //Comprobamos si el bastidor no existe en la base de datos...
            if(!Conexiones.cargar_bastidores().contains(textfield_bastidor.getText())){
                //Depende de qué radiobutton pulsemos...
                if(radiobutton_alquiler.isSelected()){
                    //Excepción por si el valor introducido no es un double.
                    double precio = 0.0;
                    try{
                        precio = Double.parseDouble(textfield_precio.getText());
                    }catch(NumberFormatException nfe_precio){
                        nfe_precio.getMessage();
                    }
                    boolean disponible = checkbox_disponible.isSelected();
                    Vehiculo va = new Vehiculo_Alquiler(precio, true, bastidor, color, matricula, modelo, 
                                                        marca, potencia, consumo, f, descripcion);
                    //Llamamos al método alta_vehiculo() para insertar el vehículo en la base de datos.
                    Conexiones.alta_vehiculo(va);
                }else{
                    //Excepción por si el valor introducido no es un double.
                    double precio = 0.0;
                    try{
                        precio = Double.parseDouble(textfield_precio.getText());
                    }catch(NumberFormatException nfe_precio){
                        nfe_precio.getMessage();
                    }
                    Vehiculo vc = new Vehiculo_Compra(precio, null, bastidor, color, matricula, modelo, 
                                                      marca, potencia, consumo, f, descripcion);
                    //Llamamos al método alta_vehiculo() para insertar el vehículo en la base de datos.
                    Conexiones.alta_vehiculo(vc);
                }
            }else{
                JOptionPane.showMessageDialog(null, "¡¡¡Este bastidor ya EXISTE en la base de datos!!!", "ATENCIÓN ADMINISTRADOR", JOptionPane.WARNING_MESSAGE);
                textfield_bastidor.requestFocus();
            }
        }else{
            JOptionPane.showMessageDialog(null, "¡¡¡Debes rellenar TODOS los campos!!!", "ATENCIÓN ADMINISTRADOR", JOptionPane.WARNING_MESSAGE);;
            textfield_bastidor.requestFocus();
        }
    }    
    
asked by omaza1990 08.01.2017 в 21:17
source

1 answer

3

The first thing you should do is to avoid trying to format the date when the value of the textfield_fecha_fabricacion field is null or empty.

Try to do the following in that line of code:

if(!textfield_fecha_fabricacion.getText().equals(""))
{
    String fecha_fabricacion = textfield_fecha_fabricacion.getText();
    //Damos formato a la fecha a insertar. Por ejemplo: 2010/10/10.
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
    Date f = null;
    try {
        //Convertimos a Date la fecha recogida como String.
        f = sdf.parse(fecha_fabricacion);
    } catch (ParseException ex) {
        Logger.getLogger(Alta_vehiculo.class.getName()).log(Level.SEVERE, null, ex);
    }
}
    
answered by 08.01.2017 / 21:47
source