1

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();
    }
Community
  • 1
  • 1
user1523885
  • 101
  • 1
  • 7

1 Answers1

0

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

kolossus
  • 20,559
  • 3
  • 52
  • 104
  • This still comes up with an error after the changes. com.sun.faces.lifecycle.ApplyRequestValuesPhase execute #{DownloadBean.doDownload}: javax.faces.el.EvaluationException: java.lang.NullPointerException javax.faces.FacesException: #{DownloadBean.doDownload}: javax.faces.el.EvaluationException: java.lang.NullPointerException – user1523885 Feb 20 '13 at 15:22
  • If I try to do a println on the response.getContentType() right after I set it, it returns nothing. So it is actually my response that is null because the getContentType() returns nothing. How can the response be null? – user1523885 Feb 20 '13 at 17:40
  • @user1523885, if `response==null`, then `response.getContentType()` will throw an NPE. Try moving `fc.responseComplete();` up by a line. – kolossus Feb 20 '13 at 18:47
  • No change. A response.getContentType() prints nothing and still throws the same error. @kolossus – user1523885 Feb 20 '13 at 19:18
  • @user1523885, give some more context to the code, is this within a managed bean? Is this in response to a commandButton `action` etc. – kolossus Feb 20 '13 at 19:35
  • Yes, this is response to hx:commandExButton action="#{DownloadBean.doDownload}" – user1523885 Feb 20 '13 at 19:51