Java NullPointerException上载文件(struts2+;hibernate)
我收到一个空指针异常,似乎无法解决它。。我正在使用Struts2和hibernate 在我的行动中:Java NullPointerException上载文件(struts2+;hibernate),java,hibernate,struts2,websphere,Java,Hibernate,Struts2,Websphere,我收到一个空指针异常,似乎无法解决它。。我正在使用Struts2和hibernate 在我的行动中: public String profilePhoto() { String pathToSaveInDB = null; String uploadFolder = null; String filePath = null; try { uploadFolder = "profile_pic"; filePath = servle
public String profilePhoto() {
String pathToSaveInDB = null;
String uploadFolder = null;
String filePath = null;
try {
uploadFolder = "profile_pic";
filePath = servletRequest.getSession().getServletContext().getRealPath("/") + uploadFolder;
System.out.println("Server path:" + filePath);
File fileToCreate = new File(filePath, this.userImageFileName);
System.out.println(this.userImageFileName);
System.out.println(this.userImage);
pathToSaveInDB = uploadFolder + "/" + this.userImageFileName;
FileUtils.copyFile(this.userImage, fileToCreate);
System.out.println("user id: " + getId());
System.out.println("photo path: " + pathToSaveInDB);
} catch (IOException e) {
e.printStackTrace();
}
profileDAO.updateProfilePhoto(Long.parseLong("1"), "test"); <==== just testing
return SUCCESS;
}
我的表格:
<s:form action="profilePic" method="post" enctype="multipart/form-data">
<label for="userImage">Profile Avatar:</label> <br />
<s:file name="userImage" label="User Image" />
<s:set name="userId" value="#session.User.id"/>
<s:hidden name="id" value="%{#userId}" />
<s:submit value="Upload" />
</s:form>
该错误特别指出profileDAO.updateProfilePhoto(Long.parseLong(“1”),“test”)代码>在my ProfileAction和transaction.rollback()中代码>在我的档案中DAOImpl是原因。。提前感谢您的帮助:)
如果你需要我发布更多的代码,一定要让我知道。干杯~我想你的交易是空的。您从哪里开始事务处理?我认为您的拦截器配置得不好
我得到了这种问题,并通过以下代码清除了
行动方法:
public String execute() {
try {
String filePath = servletRequest.getSession().getServletContext().getRealPath("/");
System.out.println("Server path:" + filePath);
File fileToCreate = new File(filePath, this.userImageFileName);
FileUtils.copyFile(this.userImage, fileToCreate);
} catch (Exception e) {
e.printStackTrace();
addActionError(e.getMessage());
return INPUT;
}
return SUCCESS;
}
struts.xml:
<action name="userImage">
class="FileUploadAction">
<interceptor-ref name="fileUpload">
<param name="maximumSize">2097152</param>
<param name="allowedTypes">
image/png,image/gif,image/jpeg,image/pjpeg
</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="success">SuccessUserImage.jsp</result>
<result name="input">UserImage.jsp</result>
class=“FileUploadAction”>
2097152
image/png,image/gif,image/jpeg,image/pjpeg
SuccessUserImage.jsp
UserImage.jsp
我对struts2和hibernate的概念还很陌生。我猜你的问题的答案是ProfileDAOImpl?哦,我正在使用Hibernate完整插件。这也许可以解释一下代码?我不能理解的是,我正在使用几乎完全相同的代码来更新我的用户配置文件,它可以工作,但在这种情况下不起作用。我猜是我的文件上传表单页面导致了这个错误。。猜猜看。com.fotoalbum.dao.ProfileDAOImpl
类的第95行是什么?另外,在updateProfilePhoto()
中,您在哪里创建会话和事务对象
public String execute() {
try {
String filePath = servletRequest.getSession().getServletContext().getRealPath("/");
System.out.println("Server path:" + filePath);
File fileToCreate = new File(filePath, this.userImageFileName);
FileUtils.copyFile(this.userImage, fileToCreate);
} catch (Exception e) {
e.printStackTrace();
addActionError(e.getMessage());
return INPUT;
}
return SUCCESS;
}
<action name="userImage">
class="FileUploadAction">
<interceptor-ref name="fileUpload">
<param name="maximumSize">2097152</param>
<param name="allowedTypes">
image/png,image/gif,image/jpeg,image/pjpeg
</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="success">SuccessUserImage.jsp</result>
<result name="input">UserImage.jsp</result>