Vaadin request to download pdf returns null pointer exception

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):

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(“after add content”);
document.close();
} catch (Exception e) {
e.printStackTrace(); }

Here is how i try to make the request for download:
if(_ui!=null){
getUI().getPage().open(“http://localhost:8080/project/download?ext=.pdf”, “_blank”, false); }

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

and here is the code i have to handle the request:

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 } }); }

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

Where are you invoking getUI().getPage().open(…)? In a button or link click listener?

Some questions to better understand your needs:
Why did you choose to implement a RequestHandler to manage the download?
What about moving you PDF generation code in a
StreamResource.StreamSource
and then use
a Link component that points to a StreamSource (or use
FileDownloader
extension if you don’t want a link)?

HTH
Marco