Reputation: 673
I'm creating a employee login validation class where i can insert their to mysql database their information then for validation i'm trying to select only the password of the row. here's my code:
import java.sql.*;
import java.sql.PreparedStatement;
import java.sql.Connection;
public class Employee
{
private PreparedStatement statement;
private Connection con;
private String pass;
public Employee()
{
}
public Employee(String firstName, String lastName, String userID, String userPassword, String role )
{
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
statement = con.prepareStatement("Insert into employee(firstName, lastName, userID, " +
"password, role)values(?, ?, ?, ?, ?)");
statement.setString(1, firstName);
statement.setString(2, lastName);
statement.setString(3, userID);
statement.setString(4, userPassword);
statement.setString(5, role);
statement.executeUpdate();
con.close();
}
catch(Exception e)
{
}
}
public void setPassword(String userID)
{
try
{
statement = con.prepareStatement("SELECT * from employee WHERE userID = ?");
statement.setString(1, userID);
ResultSet rs = statement.executeQuery();
while(rs.next()){
pass = rs.getString(4);
}
}
catch(Exception e)
{
System.out.print(e);
}
}
public String getPassword()
{
return pass;
}
}
and here is my main
public class TestMain
{
public static void main(String argsp[])
{
Employee emp = new Employee();
emp.setPassword("ecka12");
}
}
i got a null pointer exception i don't know why.
Upvotes: 0
Views: 725
Reputation: 3560
1.Make sure the employee table
is created before you are executing your code , because you have n't created the table in java code .
2.Employee(String,,,,)
constructor have not used also ,because of this Connection did not get instantiated, you have closed the connection in constructor itself . so it may exist before searching for data in your table.
Upvotes: 0
Reputation: 213261
You are creating object using: -
Employee emp = new Employee();
In your 0-arg constructor you have not initialized your connection
public Employee() {
}
So, when you use con
in setPassword()
method, you will get NullPointerException
: -
// con is not initialized
statement = con.prepareStatement("SELECT * from employee WHERE userID = ?");
statement.setString(1, userID);
To correct this, you need to
move the below code to 0-arg constructor
.. And in your other constructor: - add this()
as your first statement..
public Employee() {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","");
} catch (Exception e) {
e.printStackTrace();
}
}
public Employee(String firstName, String lastName) {
this(); // Initialize con..
try {
// You don't need to initialize your con here again..
// Your remaining code..
}
}
Upvotes: 1
Reputation: 7623
the null pointer exception happens because you have an extra constructor public Employee() , con is not initializing therefore is causing a null pointer exception.
Good Luck!
Upvotes: 0