0

我正在尝试在数据库中插入图片的详细信息。每当我提交数据时,我的 PictureBean 的第 52 行都会出现空点异常,它表明 UploadedFile.getSize() 正在返回空值。图片的其他细节没有造成任何问题。

这是我的 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>

这是我的图片豆

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
4

1 回答 1

1

它显示 uploadFile.getSize() 正在返回 null。

不,它没有返回null,这不可能导致这个特定的堆栈跟踪。uploadedFile本身null就是。因此它不指向任何东西,因此任何使用句点运算符访问/调用它的尝试.都将导致NullPointerException,正如它的 javadoc提示我们的那样。

这种特殊情况下,如果 bean 是请求范围的并且文件实际上是在不同的 HTTP 请求中上传的,则可能会发生这种情况。您之前的问题中已经给出了两种可能的解决方案:图像未更新

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

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

if (uploadedFile != null) {
    picture.setPictureSize(uploadedFile.getSize());
}
于 2012-09-14T18:58:19.167 回答