'MySQL error Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
This is my register page:
Registration was working before adding Email checker functionality. This function for check did someone used this email on database.
This is my code:
try {
Class.forName("com.mysql.jdbc.Driver");
sqlConn = DriverManager.getConnection(connect, username, password);
if (check_email(u.getEmail())) {
Class.forName("com.mysql.jdbc.Driver");
sqlConn = DriverManager.getConnection(connect, username, password);
String query = "insert into users_table (Name_surname,E_mail,Age,Profession,Student,Password)"
+ " values (?,?,?,?,?,?)";
pst = sqlConn.prepareStatement(query);
pst.setString(1, u.getName_surname());
pst.setString(2, u.getEmail());
pst.setInt(3, u.getAge());
pst.setString(4, u.getProffesion());
pst.setString(5, u.getStudent());
pst.setString(6, u.getPassword());
pst.executeUpdate();
JOptionPane.showMessageDialog(null, "You signed up");
} else {
JOptionPane.showMessageDialog(null, "You have already account");
}
} catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(Register.class.getName()).log(Level.SEVERE, null, ex);
}
public boolean check_email(String username) throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
sqlConn = DriverManager.getConnection(connect, username, password);
boolean e_mail = true;
String query = "Select * from users.users_table where E_mail = ?";
try {
pst = sqlConn.prepareStatement(query);
pst.setString(1, j_register_mail.getText());
rs = pst.executeQuery();
if (rs.next()) {
e_mail = false;
}
} catch (SQLException ex) {
Logger.getLogger(Register.class.getName()).log(Level.SEVERE, null, ex);
}
return e_mail;
This is my database:
I got this error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at JFrame.Proje.Register.check_email(Register.java:462)
at JFrame.Proje.Register.register_btActionPerformed(Register.java:430)
at JFrame.Proje.Register.access$400(Register.java:30)
at JFrame.Proje.Register$5.actionPerformed(Register.java:334)
Solution 1:[1]
Within the method check_email
you are connecting to the database using username
and password
.
But you do have a local variable/parameter with the same name username
which prevents getConnection
from seeing/using the correct global username
.
And why create a new DB-connection within the check_email
-method instead of passing the connection as a parameter to the method or using a class-variable for the connection?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Matthias Radde |