Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java NullPointerException上载文件(struts2+;hibernate)_Java_Hibernate_Struts2_Websphere - Fatal编程技术网

Java NullPointerException上载文件(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

我收到一个空指针异常,似乎无法解决它。。我正在使用Struts2和hibernate

在我的行动中:

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>