What is a NullPointerException, and how do I fix it? (12个答案)
2年前关闭。
(背景:我的新高级开发人员想了解我的Java技能,他给了我一个导致错误的程序。他要我解释为什么会发生错误以及如何解决错误。
我运行程序,发现错误是java.lang.NullPointerException,它还显示了stacktrace。据我所知(也许我错了),通常第一条消息表明该类中发生了错误,就我而言,它显示
java.lang.NullPointerException at action.userMaint.AdminUserMaintAction.save(AdminUserMaintAction.java:301)
我要求新的高级开发人员提供源代码,因为我想查看AdminUserMaintAction类中的代码
在发布问题之前,我已阅读有关错误的文章
What is a NullPointerException, and how do I fix it?我注意到有关此错误的其他文章,所以大致上我知道null指针异常是因为对象或某些内容为null。
但是,我仍然不知道如何解决该异常,因此我决定在此处发布问题)
内容
在AdminUserMaintAction类中,我找到了save()方法。这是代码。
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();
}
从堆栈跟踪中,此行中发生错误
adminUserMaintService.insert(admFunc);
我在那里找到一个名为AdminUserMaintService的类,并且其中存在insert方法。这是代码
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();
}
我阅读了代码,看起来不错,而且我不知道为什么会发生错误,因为没有代码指示返回空值。
另外,在AdminUserMaintService类中我不了解一件事,insert方法具有硬代码值,例如“ loginid”,“ 3”,“ created by”和“ modified by”,并且仍然会得到null指针异常?
我认为问题出在AdminUserMaintAction类中,因为stacktrace指示那里存在错误
在AdminUserMaintAction类中,我使用System.out.println在代码中打印值。这是我在代码中输入的内容
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();
}
另外,我将硬代码值放在AdminUserMaintService类的insert方法中。
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);
}
}
但是,当我运行程序时,它仍然给我空指针异常。
我使用System.out.println进行打印,我注意到该值不为null。我不明白为什么会收到异常,我在AdminUserMaintAction和AdminUserMaintService类中都对该值进行了硬编码。
所以我想问一些问题,
硬编码值是否为空? (据我所知,我不认为硬编码的值等于null,因为使用System.out.println可以打印该值。但是在我看来,它似乎仍然不同,它仍然显示null指针异常,指示实际的位置.userMaint.AdminUserMaintAction.save())
由于我注意到在action.userMaint.AdminUserMaintAction.save()中指示异常发生的堆栈跟踪,因此我转到该类尝试打印值的方法,并且可以看到该值,所以这意味着我可以确认对象不为空?
如果对象不为null,有什么办法可以解决null指针异常?
谢谢你的时间。
编辑
感谢大家的评论和回答,我注意到adminUserMaintService为null,并添加了一些代码使其不为null。因此,当我运行代码时,尽管它仍然显示空指针异常,但它表明该异常位于
Connection conn = jdbcTemplate.getDataSource().getConnection();
这意味着adminUserMaintService目前不为空。
因此,现在我将研究为什么空指针会更改位置。
谢谢。