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.