Vaadin request to download pdf returns null pointer exception
<p>I am trying to create and then download a pdf file in my Vaadin application (generated with vaadin-archetype-application), but somehow I can't figure out why it isn't working and why i get the exception
Here is how i have built the pdf file (using iText):</p>

<pre><code>String fileName = "file.pdf";
String relativePath = VaadinServlet.getCurrent().getServletContext().getRealPath("");

    try {
        Document document = new Document(PageSize.LETTER.rotate());

        PdfWriter.getInstance(document, new FileOutputStream(relativePath + "\\" + fileName));
        document.open();
        table = new PdfPTable(6);
        addMetaData(document);
        addTitlePage(document);
        addContent(document, table);
        System.out.println("dupa add content la scrierea documentului pdf");
        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
</code></pre>

<p>Here is how i try to make the request for download:</p>

<pre><code>if(_ui!=null){
    getUI().getPage().open("http://localhost:8080/project/download?ext=.pdf", "_blank", false);
}
</code></pre>

<p>This it the line where i get the null pointer exception
I surrounded it in an if clause because at first i thought the _ui argument was null</p>

<p>and here is the code i have to handle the request:</p>

<pre><code>private void registerRequestHandler() {
    // A request handler for generating some content
    VaadinSession.getCurrent().addRequestHandler(new RequestHandler() {
        private static final long serialVersionUID = 1L;

        @Override
        public boolean handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response)
                throws IOException {
            System.out.println("req: "+request.getPathInfo());         //not shown in console
            if ("/download".equals(request.getPathInfo())) {

                // reads input file from an absolute path
                String extension = request.getParameter("ext");
                System.out.println("extension: "+extension);
                String filePath = VaadinServlet.getCurrent().getServletContext().getRealPath("") + "\\file"
                        + extension;
                File downloadFile = new File(filePath);
                FileInputStream inStream = new FileInputStream(downloadFile);

                // if you want to use a relative path to context root:
                System.out.println("filePath = " + filePath);

                // obtains ServletContext
                ServletContext context = VaadinServlet.getCurrent().getServletContext();

                // gets MIME type of the file
                String mimeType = context.getMimeType(filePath);
                if (mimeType == null) {
                    // set to binary type if MIME mapping not found
                    mimeType = "application/octet-stream";
                }
                System.out.println("MIME type: " + mimeType);

                // modifies response
                response.setContentType(mimeType);
                response.setContentLength((int) downloadFile.length());

                // forces download
                String headerKey = "Content-Disposition";
                String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());
                response.setHeader(headerKey, headerValue);

                // obtains response's output stream
                OutputStream outStream = response.getOutputStream();

                byte[] buffer = new byte[4096];
                int bytesRead = -1;

                while ((bytesRead = inStream.read(buffer)) != -1) {
                    outStream.write(buffer, 0, bytesRead);
                }

                inStream.close();
                outStream.close();

                return true; // We wrote a response
            } else
                return false; // No response was written
        }
    });
}
</code></pre>

<p>This part of code is in my MyUi class. None of the system.out.printlns gets printed.
Any help/ideas/suggestions is deeply appreciated. </p>