0

<h:inputFile/> return a NullPointerException error. I know there is already a lot of stuff about this exception and uploading files with JSF but I've read it all without any success to diagnose this problem.

I am using JSF 2.2 with GlassFish 4.1.1, without PrimeFaces or Tomahawk since you can handle the file upload with JSF 2.2. as I read.

The goal is just to upload a picture on the remote filesystem, and copy the path to the DB to retrieve it later through a datatable.

I've already checked :

1 - My form has the enctype="multipart/form-data" attribute.

2 - My form is not nested in another form though it is part of <ui:composition/>.

3 - I have set all proper filter to process multipart/form-data type :

<filter>
    <filter-name>MyFacesExtensionsFilter</filter-name>
    <filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>MyFacesExtensionsFilter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

4 - When I remove the <h:inputFile/> tag and its part in the create() method, sending data works without any problem.

5 - I tried to state the ManagedBean as @ApplicationScoped, @ViewScoped and @RequestScoped without any change.

6 - I tried to put the initialize Part attribute in the ManagedBean with abstract methods without any success.


Here is my form :

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:f="http://xmlns.jcp.org/jsf/core"
                xmlns:h="http://xmlns.jcp.org/jsf/html"
                xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
                xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
                xmlns:t="http://myfaces.apache.org/tomahawk">
    <h:form enctype="multipart/form-data">
        <h:outputLabel value="Marque : " />
        <h:inputText value="#{carController.car.brand}" />
        <br/>
        few same inputText
        <h:outputLabel value="Photo : " />
        <h:inputFile value="#{carController.picture}" required="true" requiredMessage="No file selected."/>
        <br/>
        <h:commandButton value="Valider" action="#{carController.create}" />
    </h:form>
</ui:composition>

Here is my ManagedBean, I've let comments as it is other ways to process the image upload that I've tried :

@Named(value = "carController")
@SessionScoped
public class CarController implements Serializable {
    private static final long serialVersionUID = 1L;
    @EJB
    private CarFacade carFacade;

    private Car car;
    private Part picture;

    private Collection<Car> carCollection;

    public CarController() {car = new Car();}

    public String create() throws IOException {

    /*try (   InputStream inputStream = picture.getInputStream();
            FileOutputStream outputStream = new FileOutputStream("D:" +
            File.separator + "test" + File.separator +
            picture.getSubmittedFileName())) {
        int bytesRead = 0;
        final byte[] chunck = new byte[1024];
        while ((bytesRead = inputStream.read(chunck)) != -1) {
            outputStream.write(chunck, 0, bytesRead);
        }
    }*/

    /*Path folder = Paths.get("//resources//cars//");
    String filename = FilenameUtils.getName(picture.getName()); 
    String extension = FilenameUtils.getExtension(picture.getName());
    Path file = Files.createTempFile(folder, filename + "-", "." + extension);
    try (InputStream input = picture.getInputStream()) {
        Files.copy(input, file, StandardCopyOption.REPLACE_EXISTING);
    }*/
    picture.write("\\resources\\cars\\"+picture.getSubmittedFileName()+"."+picture.getContentType());
    car.setPicturePath(picture.getSubmittedFileName());
    carFacade.create(car);
    return "/index.xhtml";
}

Here is exception :

Caused by: java.lang.NullPointerException
    at Controller.CarController.create(CarController.java:57)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:289)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
    ... 41 more

Thank you for any idea that may help.

EDIT :

THIS is responsible of the error : picture.write("\resources\cars\"+picture.getSubmittedFileName()+"."+picture.getC‌​ontentType());

I've tried to add this to web.xml without any success :

<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <multipart-config>
    <location>\resources\cars\</location>
    <max-file-size>1300000</max-file-size>
    <max-request-size>20000000</max-request-size>
    <file-size-threshold>50000</file-size-threshold>
    </multipart-config>
</servlet>
Paul
  • 1,410
  • 1
  • 14
  • 30
  • Get rid of `MyFacesExtensionsFilter` and retry. That's only for pre-JSF 2.2. That you got your webapp to successfully deploy with that filter in turn suggests that you actually have Tomahawk or MyFaces in runtime classpath on the contrary to what you stated yourself. – BalusC Feb 11 '16 at 14:52
  • It returns a fileNotFound exception now because I tried with an absolute path that doesn't exist, problem solved I guess! Thank you very much! :) I would like to markup this as the solution for next people in trouble but I can't set your comment as the correct answer! – Paul Feb 11 '16 at 14:57

1 Answers1

0

Remove the MyFacesExtensionsFilter. That's only necessary when you're using <t:inputFileUpload> instead of <h:inputFile>.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555