There are two methods in different classes
public EmployeeBean viewEmployeeByID(String EmployeeID)
public EmployeeBean findByID(String id)
Given, the description of each method of what it supposed to do
public EmployeeBean findByID(String id)
This method should use the JDBC select statements to retrieve the record based on the
given Employee ID,
Store the fields in the EmployeeBean and return the Employee bean.
In case of any JDBCExceptions or if the id does not exist in the database then a null value
needs to be returned
public EmployeeBean viewEmployeeByID(String EmployeeID)
This method should retrieve the data from the employee table for the EmployeeID passed, by following the below steps.
Invoke the findByID(String id) method of the EmployeeDAO class.
If record is found, the details should be stored in a EmployeeBean object and returned.
If record is NOT found or has INVALID empID (such as an empty string “”), then the EmployeeBean should be assigned null and returned.
Code For above methods i have written
public EmployeeBean findById(String id){
EmployeeBean emp = null;
try{
statement = DBUtil.getDBConnection().prepareStatement("SELECT * FROM EMPLOYEE_TBL WHERE EMPID=?");
statement.setString(1,id);
if(statement.executeUpdate() == 1 ){
resultSet = statement.getResultSet();
while(resultSet.next()){
emp = new EmployeeBean(
resultSet.getString(1),
resultSet.getString(2),
resultSet.getDate(3),
resultSet.getString(4),
resultSet.getFloat(5)
);
}
return emp;
}
else{
return null;
}
}catch(SQLException e){
return null;
}catch(NullPointerException e){
return null;
}
}
public EmployeeBean viewEmployeeByID(String empId)throws NullPointerException{
if((employeeBean = this.database.findById(empId)) != null)
return employeeBean;
return (EmployeeBean = null);
}
If i try to find an ID which is not present in the database , it returns null and i get null pointer exception .
Questions
1) Did my snippet statisfy all the requirements given above for each method . if it is not so , please let me know
2) is there any way to avoid null pointer exception ?