I can not add a record to my database, mark error PreparedStatement [duplicated]

1

I am trying to add data to a database using a button, but at the moment of entering the data I get an error in the Prepared Statement part and my SQL statement I tried it on my local server (phpmyadmin) and it worked perfection, and I have not been able to advance in my project because of that error.

I leave part of my code:

This is when you click the add button to the database

    conexionBD cc = new conexionBD();
    Connection cn=cc.conectar;
    String sql="";

    if(rbnHerraOfi.isSelected())
        tip=rbnHerraOfi.getText();
    else
        tip=rbnArtPape.getText();
    sql="INSERT INTO productos (Articulo,Precio_compra,Precio_venta,Stock_min,TipoArticulo,Cantidad) VALUES (?,?,?,?,?,?)";

    try {
         PreparedStatement pst = cn.prepareStatement(sql);
         pst.setString(1,txt_NombrePro.getText());
         pst.setDouble(2,Double.parseDouble(txtPrecioCompraPro.getText()));
         pst.setDouble(3,Double.parseDouble(txtPrecioVentaPro.getText()));
         pst.setInt(4,Integer.parseInt(txtStockMinimo.getText()));
         pst.setString(5,tip);
         pst.setInt(6,Integer.parseInt(txtCantidadPro.getText()));

         int n=pst.executeUpdate();
         if(n>=0){
             JOptionPane.showMessageDialog(null,"Registros Guardados con Exito");
              limpiar();
         }

    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(null,""+ex);

    }
        '

And this is what I have in my connection class

public class conexionBD {
    Connection conectar=null;

    public Connection conectar() {

        try {
            Class.forName("org.gjt.mm.mysql.Driver");
            conectar=DriverManager.getConnection("jdbc:mysql://localhost/papeleria","root","");
            JOptionPane.showMessageDialog(null, "Conectado correctamente");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error al conectarse a la base de datos"+e);
        }
        return conectar;
    }
}

This is the error I receive:

  

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException     at pepeleria2.Admin2.btnGuardarProActionPerformed (Admin2.java:650)

Line 650 is exactly where the PreparedStatement is.

    
asked by Sebastian 22.10.2017 в 08:40
source

1 answer

-1

The String sql=""; statement creates an empty SQL statement to, depending on the if loop, start it or not depending on whether rbnHerraOfi.isSelected() or not. The if statement works perfectly despite not having the inner code in square brackets because it only contains one statement. But the else statement contains exactly 2 lines. It is discouraged not to enter brackets, because as in your case, if you exceed the number of sentences greater than 1, only the first will be executed. Add brackets to that statement if like this:

if(rbnHerraOfi.isSelected()) {
        tip=rbnHerraOfi.getText();
   } else {
        tip=rbnArtPape.getText();
        sql="INSERT INTO productos (Articulo,Precio_compra,Precio_venta,Stock_min,TipoArticulo,Cantidad) VALUES (?,?,?,?,?,?)";
   }
    
answered by 22.10.2017 в 08:59