简体   繁体   中英

Java Mysql Nullpointer exception

I am trying to feed data into a MySQL table using a PreparedStatement , the problem is that I get a NullPointerException when it reaches the pst.setInt line.

    Conexion con = new Conexion();
    Connection reg = con.conexion();
    Integer ced;
    boolean f = false;        

    String nom, ape, email, dir, tel, codtel, telefono;
    Date fechareg = new Date();
    String sql;

    ced = Integer.getInteger(CedulaField.getText());
    nom = NombreField.getText();
    ape = ApellidoField.getText();
    email = emailField.getText();
    dir = dirArea.getText();
    tel = TelField.getText();
    codtel = (String) TelBox.getSelectedItem();
    telefono = codtel + tel;        

    tel = TelField.getText();
    sql = "INSERT INTO estudiantes (cedula, nombre, apellido, email, direccion, telefono, fecha_registro) VALUES (?,?,?,?,?,?,?)";
    try {
        PreparedStatement pst = reg.prepareStatement(sql);
        pst.setInt(1,ced);
        pst.setString(2,nom);
        pst.setString(3,ape);
        pst.setString(4,email);
        pst.setString(5,dir);
        pst.setString(6,telefono);
        pst.setDate(7, (java.sql.Date) fechareg);

        //if (f == false){
            int n = pst.executeUpdate();
            if (n > 0){
                JOptionPane.showMessageDialog(null,"registrado con exito");
            }
        //}
    } catch (SQLException ex) {
        Logger.getLogger(Ingreso_Datos.class.getName()).log(Level.SEVERE, null, ex);
        JOptionPane.showMessageDialog(null,"Error al registrar");
    }

Here is my estudiantes table

cedula          | int(11) unsigned zerofill | NO | PRI | NULL
nombre          | varchar(50)               | NO |     | NULL
apellido        | varchar(50)               | NO |     | NULL 
email           | varchar(100)              | NO |     | NULL
direccion       | varchar(100)              | NO |     | NULL 
telefono        | varchar(11)               | NO |     | NULL 
fecha_registro  | timestamp                 | NO |     | CURRENT_TIMESTAMP 

If I am giving ced a value, why am I getting a NullPointerException ? The cedula is an id that has a number that's unique to every registered citizen, so I was planning to use it as a primary key. What am i doing wrong?

David Maust pointed out the difference between Integer.getInteger and Integer.parseInt that was were the problem was.

It seems that this line is returning null .

ced = Integer.getInteger(CedulaField.getText());

According to the javadoc this method retrieves the value of the system property by name. https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#getInteger(java.lang.String)

I think you meant to use is parseInt , which parses the string as an integer:

ced = Integer.parseInt(CedulaField.getText());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM