Hi, I would like to seek for help because right now I am very confused about why my code is not working..
The function of my code will add records in my database (MySQL), Swing components of my frame..
btnUserAdd = jButton
txtUname, txtPword, txtLname, txtFname, txtMname, txtEmpNo = jTextField
cbAccType = jComboBox
EmpNo, Uname, Pword, Lname, Fname, Mname, AccType = MySQL fields
I created a method to be called when btnUserAdd is clicked. here is the code for the method:
public void UserAdd(){
String sql = "SELECT * FROM User WHERE Uname = ? and EmpNo = ?";
try {
pst = conn.prepareStatement(sql);
pst.setString(1, txtUname.getText().toString());
pst.setString(2, txtEmpNo.getText().toString());
rs = pst.executeQuery();
if (rs.next()){
JOptionPane.showMessageDialog(null, "Record Exists! Please Try Again.");
txtUname.requestFocus();
cbAccType.setSelectedItem("Operator");
UserClear();
}
else {
try {
String sql1 = "INSERT INTO user (EmpNo, Uname, Pword, Lname, Fname, Mname, AccType)" + "VALUES (?, ?, ?, ?, ?, ?, ?)";
String cb1 = cbAccType.getSelectedItem().toString();
pst = conn.prepareStatement(sql1);
pst.setString(1, txtEmpNo.getText());
pst.setString(2, txtUname.getText());
pst.setString(3, txtPword.getText());
pst.setString(4, txtLname.getText());
pst.setString(5, txtFname.getText());
pst.setString(6, txtMname.getText());
pst.setString(7, cb1);
pst.execute();
JOptionPane.showMessageDialog(null, "Record Added!");
}
catch (Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
}
catch (Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
then here is the code inside the btnUserAdd:
btnUserAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (txtUname.getText().trim().equals("")||txtPword.ge tText().trim().equals("")||txtEmpNo.getText().trim ().equals("")||txtLname.getText().trim().equals("" )){
JOptionPane.showMessageDialog(null, "Please Enter a Username, Password and Employee No, Try Again!");
if (txtUname.getText().trim().equals("")){
txtUname.requestFocus();
}
else if (txtPword.getText().trim().equals("")){
txtPword.requestFocus();
}
else if (txtLname.getText().trim().equals("")){
txtLname.requestFocus();
}
else if (txtEmpNo.getText().trim().equals("")){
txtEmpNo.requestFocus();
}
}
else{
UserAdd();
}
}
});
the error is that it does not add the record it will display JAVA.LANG.NULLPOINTEREXCEPTION...
please help me with my problem..