1
votes

I am trying to do a csv download in the same fashion as here: How to provide a file download from a JSF backing bean?

My response keeps throwing a nullPointerException at the output.write() line. The bean is of the request scope. Any thoughts as to the null pointer?

    try
    {
        //submitForm();
        FacesContext fc = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse();

        response.reset();
        response.setContentType("text/csv"); 
        //response.setContentLength(contentLength); 
            response.setHeader ( "Content-disposition", "attachment; filename=\"Reporting-" + 
                    new Date().getTime() + ".csv\"" );

        OutputStream output = response.getOutputStream();
        String s = "\"Project #\",\"Project Name\",\"Product Feature(s)\",";
        s+="\"Project Status\",";
        s+="\"Install Type\",";
        s+="\"Beta Test\",\"Beta Test New/Updated\",";
        s+="\"Production\",\"Production New/Updated\",";
        s+="\n";
        InputStream is = new ByteArrayInputStream( s.getBytes("UTF-8") );
        int nextChar;

         while ((nextChar = is.read()) != -1) 
         {
            output.write(nextChar);
         }
         output.close();

    }
    catch ( IOException e )
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
1
This is not a JSF direct problem. You need to convert String into a Stream. Also, don't forget to close the output. - Luiggi Mendoza
Modified to InputStream and sent to OutputStream and it is still throwing a nullPointer at output.write. Thoughts? - user1523885
Your output variable has a null value, what is very strange. - Luiggi Mendoza
Are you using portlets or so? - BalusC

1 Answers

0
votes

3 things jump out here

  1. Failing to call responseComplete() on FacesContext pretty much means JSF will continue to process the request and you don't get to affect the outcome.

  2. The reset() call is unnecessary.

  3. The outputstream should be of type ServletOutputStream

    Try the following snippet instead

    try
    {
    //submitForm();
    FacesContext fc = FacesContext.getCurrentInstance();
    HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse();
    
    
    response.setContentType("text/csv"); 
    fc.responseComplete();
    //response.setContentLength(contentLength); 
        response.setHeader ( "Content-disposition", "attachment; filename=\"Reporting-" + 
                new Date().getTime() + ".csv\"" );
    
    ServletOutputStream output = response.getOutputStream();
    String s = "\"Project #\",\"Project Name\",\"Product Feature(s)\",";
    s+="\"Project Status\",";
    s+="\"Install Type\",";
    s+="\"Beta Test\",\"Beta Test New/Updated\",";
    s+="\"Production\",\"Production New/Updated\",";
    s+="\n";
    InputStream is = new ByteArrayInputStream( s.getBytes("UTF-8") );
    int nextChar;
    
     while ((nextChar = is.read()) != -1) 
     {
        output.write(nextChar);
     }
        output.flush();
    
     output.close();
    
    }
    catch ( IOException e )
    {
     // TODO Auto-generated catch block
        e.printStackTrace();
    }
    

Additionally, you can just call sos.println(s) without the need for all that work you're doing there