我试图在我的数据库中添加一行,在表员工。但我得到一个例外:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ConnectionBD.EmployeeBD.add(EmployeeBD.java:56)`
这是我的源代码(EmployeeBD 类):
public void add() throws SQLException, ClassNotFoundException {
56 Employee a = ep.ReadEmployee();
57 if (a!=null) {
58 Connection conn = null;
try {
Class.forName(BDConnect.DRIVER);
conn = DriverManager.getConnection(BDConnect.BD_URL, BDConnect.USUARI, BDConnect.PASSWORD);
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE );
if (conn != null) {
String sql = "SELECT * FROM employee";
ResultSet rs = stmt.executeQuery( sql );
rs.moveToInsertRow();
rs.updateString( "nif", a.getNif());
rs.updateString( "name", a.getName());
rs.updateDouble( "salary",a.getSalary());
System.out.println("Added Suscefully");
rs.insertRow();
rs.moveToCurrentRow();
} else {
System.out.println("Can't get DB");
}
} catch (SQLException ioe) {
System.out.println(ioe);
} catch(ClassNotFoundException ex) {
System.out.println(ex);
}
}
}
和 ReadEmployee() 方法:
public Employee ReadEmployee() {
String nif = null;
String name = null;
Double salary = null;
int depId = 0;
nif = jTextField1.getText();
name = jTextField2.getText();
salary = Double.parseDouble(jTextField3.getText());
Employee emp = new Employee(nif, name, salary, 1);
if (name.equals("") || nif.equals("") || salary != null) {
return emp;
} else {
JOptionPane.showMessageDialog(this, "Fill all TextFields", "Article warning", JOptionPane.WARNING_MESSAGE);
return null;
}
}
为什么它会抛出异常?我不知道如何正确地做到这一点!