照片大小未在数据库中更新

时间:2012-09-14 18:42:54

标签: jsf jsf-2 nullpointerexception

我正在尝试在数据库中插入图片的详细信息。每当我提交数据时,我在PictureBean的第52行得到一个空点异常,它表明uploadedFile.getSize()返回null。图片的其他细节不会造成任何问题。

这是我的xhtml文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
<h:head>Add Photo</h:head>
<h:form enctype="multipart/form-data">
    <h:panelGrid columns="2">
                    <h:outputText value="Photo"/>
                    <p:fileUpload fileUploadListener="#{pictureBean.handleFileUpload}"/>
                    <h:outputText value="Picture ID: " />
                    <p:inputText styleClass="outputLabel"
                                value="#{pictureBean.picture.pictureId}" />
                    <h:outputText value="Picture Name: " />
                    <p:inputText styleClass="outputLabel"
                                value="#{pictureBean.picture.pictureName}" />
                    <h:outputText value="Picture Details: " />
                    <p:inputText styleClass="outputLabel"
                                value="#{pictureBean.picture.pictureDetails}" />
                    <h:outputText value="Album ID: " />
                    <p:inputText styleClass="outlabel" value="#{pictureBean.picture.albumId}"/>
                    <p:commandButton value="Add Photo" action="#{pictureBean.save}"/>
                                </h:panelGrid>
                                </h:form>
</html>

这是我的PictureBean

package com.bean;

import javax.faces.context.FacesContext;
import javax.faces.event.PhaseId;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.http.HttpSession;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

import com.entity.Picture;
import com.entity.User;

public class PictureBean {
    Picture picture;
    private UploadedFile uploadedFile;
    private EntityManager em;
    public PictureBean(){
        picture = new Picture();
        EntityManagerFactory emf=Persistence.createEntityManagerFactory("FreeBird");
         em =emf.createEntityManager();
    }
    public Picture getPicture() {
        return picture;
    }
    public void setPicture(Picture picture) {
        this.picture = picture;
    }

    public void handleFileUpload(FileUploadEvent e) {  
        System.out.println("file handler called");
        uploadedFile = e.getFile();
        String fileName=uploadedFile.getFileName();
        fileName=fileName.substring(fileName.lastIndexOf("\\")+1);
        System.out.println(fileName);
        byte[] a = uploadedFile.getContents();
        PhaseId currentPhaseId = FacesContext.getCurrentInstance().getCurrentPhaseId();
        System.out.println("current phase id"+currentPhaseId);
        picture.setPicture(a);


    }
    public String save(){
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
        User user = (User) session.getAttribute("userdet");
        picture.setEmail(user.getEmail());
        picture.setPictureSize(uploadedFile.getSize());
        System.out.println("picture save called");
        em.getTransaction().begin();
        em.persist(picture);
        em.getTransaction().commit();
        return "success";
    }
}

以下是我的例外情况。

WARNING: #{pictureBean.save}: java.lang.NullPointerException
javax.faces.FacesException: #{pictureBean.save}: java.lang.NullPointerException
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)
Caused by: javax.faces.el.EvaluationException: java.lang.NullPointerException
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    ... 36 more
Caused by: java.lang.NullPointerException
    at com.bean.PictureBean.save(PictureBean.java:52)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:254)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:302)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    ... 37 more

1 个答案:

答案 0 :(得分:1)

  

表明uploadedFile.getSize()返回null。

不,它没有返回null,这不可能导致这个特定的堆栈跟踪。 uploadedFile本身就是null。因此,它没有指向任何,因此任何使用句点.运算符访问/调用它的尝试都会产生NullPointerException,与its javadoc提示完全相同我们。

这个特定的情况下,如果bean是请求作用域并且文件实际上是在不同的HTTP请求中上传的话,就会发生这种情况。您在上一个问题中已经提供了两种可能的解决方案:Image not getting updated

  • 将bean放在视图范围内。
  • <p:fileUpload>设为mode="simple"

第三种解决方案是预先执行nullcheck。由于您没有将其设置为required="true",另一个可能的原因是最终用户根本没有选择/发送任何文件。

if (uploadedFile != null) {
    picture.setPictureSize(uploadedFile.getSize());
}