(Background: My new senior developer wants to know my java skills, he gives me a program that cause an error. He wants me to explain why the error occurs and how to solve the error.
I run the program and I find the error is java.lang.NullPointerException, it also shows the stacktrace. From my knowledge (manbe I am wrong), usually the first message indicates the error occurs in which class, in my case, it shows
java.lang.NullPointerException at action.userMaint.AdminUserMaintAction.save(AdminUserMaintAction.java:301)
I ask the new senior developer for the source code because I want to view the code in the AdminUserMaintAction class
Before I post my question, I read this post What is a NullPointerException, and how do I fix it? about the error
I notice there are some other posts about this error, so roughly I know the null pointer exception because the object or something is null.
However, I still cannot figure out the how to solve the exception so I decide to post my question here)
Content
In AdminUserMaintAction class, I find the save() method. Here is the code.
public String save(){
String curloginId = (String) session.get("_loginid");
funcid =1;
AdmFunc admFunc = new AdmFunc();
admFunc.setCreatedBy(curloginId);
admFunc.setCreatedDate(createdDate);
admFunc.setFuncid(funcid);
admFunc.setLoginid(curloginId);
admFunc.setModifiedBy(curloginId);
admFunc.setModifiedDate(modifiedDate);
try {
adminUserMaintService.insert(admFunc);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return view2();
}
From the stacktrace, the error occurs in this line
adminUserMaintService.insert(admFunc);
I find there a class called AdminUserMaintService class and the insert method is in there. Here is the code
public void insert(AdmFunc admFunc) throws SQLException{
Connection conn = jdbcTemplate.getDataSource().getConnection();
String sql = "INSERT INTO ADM_FUNC (LOGINID, FUNC_ID, CREATED_DATE, CREATED_BY, MODIFIED_DATE, MODIFIED_BY) VALUES (NULL,NULL,NULL,NULL,NULL,NULL)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, admFunc.getLoginid());
pstmt.setInt(2, admFunc.getFuncid());
pstmt.setTimestamp(3, admFunc.getCreatedDate());
pstmt.setString(4, admFunc.getCreatedBy());
pstmt.setTimestamp(5, admFunc.getModifiedDate());
pstmt.setString(6, admFunc.getModifiedBy());
pstmt.execute();
}
I read the code and it seems fine and I don't know why it occurs error because there is no code indicates to return null value.
Also, there is one thing I don't understand in the AdminUserMaintService class, the insert method has hard code value such as "loginid", 3,"created by" and "modified by" and still get the null pointer exception?
I think the problem is in AdminUserMaintAction class as the stacktrace indicates the error in there
In AdminUserMaintAction class, I use System.out.println to print value in the code. Here is what I put in the code
public String save(){
String curloginId = (String) session.get("_loginid");
funcid =1;
AdmFunc admFunc = new AdmFunc();
admFunc.setCreatedBy(curloginId);
admFunc.setCreatedDate(createdDate);
admFunc.setFuncid(funcid);
admFunc.setLoginid(curloginId);
admFunc.setModifiedBy(curloginId);
admFunc.setModifiedDate(modifiedDate);
//I try to print values to see what values inside the code
System.out.println("admfunc.setFuncid is " + admFunc.getFuncid()); //it shows 1
System.out.println("admfunc.setCreatedBy is " + admFunc.getCreatedBy() ); //it shows tester
System.out.println("admfunc.setCreatedDate is " + new Timestamp(new Date().getTime()) ); //it shows today's date and time
System.out.println("admfunc.setMofifiedBy is " + admFunc.getModifiedBy() ); //it shows tester
System.out.println("admfunc.setModifiedDate is " +new Timestamp(new Date().getTime()) ); //it shows today's date and time
System.out.println("admFunc value is" + admFunc); //it shows jpa.AdmFunc@624c08e2
System.out.println("start save ...");
System.out.println("");
//adminUserMaintService.insert(admFunc); // I try this code and it occurs the same error
// this.adminUserMaintService.insert(admFunc); // I try this code and it occurs the same error too
try {
adminUserMaintService.insert(admFunc);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("end save ...");
return view2();
}
Also, I put hard code value in the insert method in AdminUserMaintService class.
public void insert(AdmFunc admFunc) throws SQLException{
Connection conn = jdbcTemplate.getDataSource().getConnection();
String sql = "INSERT INTO ADM_FUNC (LOGINID, FUNC_ID, CREATED_DATE, CREATED_BY, MODIFIED_DATE, MODIFIED_BY) VALUES (NULL,NULL,NULL,NULL,NULL,NULL)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1,"loginid");
pstmt.setInt(2, 3);
pstmt.setTimestamp(3,new Timestamp(new Date().getTime()));
pstmt.setString(4, "created by");
pstmt.setTimestamp(5,new Timestamp(new Date().getTime()));
pstmt.setString(6, "modified by");
pstmt.execute();
try {
conn.setAutoCommit(false);
pstmt.setString(1,"loginid");
pstmt.setInt(2, 3);
pstmt.setTimestamp(3,new Timestamp(new Date().getTime()));
pstmt.setString(4, "created by");
pstmt.setTimestamp(5,new Timestamp(new Date().getTime()));
pstmt.setString(6, "modified by");
pstmt.execute();
conn.commit();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
conn.rollback();
} finally {
pstmt.close();
conn.setAutoCommit(true);
}
}
However when I run the program, it still gives me the null pointer exception.
I used System.out.println to print something, I notice the value is not null. I don't understand why I get the exception, I hard coded the value in both AdminUserMaintAction and AdminUserMaintService class.
So I have some questions would like to ask here,
Hard coded value is null? (from my knowledge, I don't think hard coded value equals to null, because use System.out.println can print the value. However in my case, it seems different, it still shows null pointer exception, indicate the location in action.userMaint.AdminUserMaintAction.save())
Since I notice the stacktrace that indicate the exception occurs in action.userMaint.AdminUserMaintAction.save(), so I go to that class, that method to try to print value and I can see the value, so does that mean I can confirm the object is not null?
If the object is not null, is there any way I can find out to solve the null pointer exception?
Thanks for your time.
Edit Thanks everyone's comments and answers, I notice adminUserMaintService is null and I put some some code it to make it not null. So when I run the code, although it still shows the null pointer exception, it shows the exception is in
Connection conn = jdbcTemplate.getDataSource().getConnection();
That means adminUserMaintService is not null at this moment.
So now I will study why the null pointer changes the location.
Thanks.