我添加了以下内容以将我的程序的注册页面连接到我的数据库,但每当我按下注册按钮测试创建帐户时都会收到错误 java.lang.nullPointerException。我认为问题是 getText 必须是 getPassword 但是当我这样做时我会收到一个方法。有人有想法么?
register = new JButton("Register");
register.setBackground(Color.black);
register.setForeground(Color.white);
register.setBounds(250, 12, 120, 30);
register.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent reg) {
if (firstName.getText().equals("") || lastName.getText().equals("") || age.getText().equals("") || user.getText().equals("") || pass.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Invalid Input Data.\nPlease Try Agian.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
try{
String sql = "INSERT INTO Users (ID, First Name, Last Name, Age, Username, Password) VALUES (?, ?, ?, ?, ?, ?)";
ps = conn.prepareStatement(sql);
ps.setInt(1, 0);
ps.setString(2, firstname.getText());
ps.setString(3, lastname.getText());
ps.setString(4, age.getText());
ps.setString(5, user.getText());
ps.setString(6, pass.getText());
ps.execute();
JOptionPane.showMessageDialog(null, "Registration Successful!/nYou can now log into the database.");
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
finally {
try{
rs.close();
ps.close();
}catch(Exception e) {
}
}
}
});