0

ヌル ポインター例外が発生し、解決できないようです。Struts2 を使用して休止状態にしています。

私の ProfileAction で:

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;

}

私のプロフィールDAOImpl:

@Override
public void updateProfilePhoto(Long id, String path) {

    String SQL_QUERY = "update Profile set ProfilePhotoPath = :path" +
        " where UserId = :id";
    try {
        System.out.println(SQL_QUERY);
        Query query = session.createQuery(SQL_QUERY);
query.setParameter("id", id);
query.setParameter("path", path);

        int result = query.executeUpdate();
        System.out.println(result);
    } catch (Exception e) {
        transaction.rollback();
        e.printStackTrace();
    } 
}

私のフォーム:

<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>

エラー:

java.lang.NullPointerException
com.fotoalbum.dao.ProfileDAOImpl.updateProfilePhoto(ProfileDAOImpl.java:95)
com.fotoalbum.web.ProfileAction.profilePhoto(ProfileAction.java:89)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
java.lang.reflect.Method.invoke(Method.java:611)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:453)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:292)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:255)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:211)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:211)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:90)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:320)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:192)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:320)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:511)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)

エラーはprofileDAO.updateProfilePhoto(Long.parseLong("1"), "test");、私の ProfileAction とtransaction.rollback();私の ProfileDAOImpl が原因であることを具体的に示しています..事前に助けてくれてありがとう:)

私のコードをもっと投稿する必要がある場合は、お知らせください。乾杯〜

4

2 に答える 2

0

あなたの取引は無効だと思います。どこから取引を開始しますか?

于 2012-07-30T18:22:33.983 に答える
0

インターセプターがうまく構成されていないと思います

この種の問題が発生し、次のコードでクリアしました.....

アクション方法:

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>
于 2013-02-14T07:09:29.877 に答える