title: getServletContext().getRealPath() doesn't work in controller (NPE), but works in jsp
body: <p>I am trying to add image to my pdf file. Image is located in "WebContent/img/image.png". First I save relative path to string and then I convert this relative path to real path.</p>
  
  <pre><code>String relativeWebPath = "/img/image.png";
  String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);
  Image image1 = Image.getInstance(absoluteDiskPath);
  </code></pre>
  
  <p>Even this</p>
  
  <pre><code>String absoluteDiskPath = getServletContext().getRealPath("/");
  </code></pre>
  
  <p>doesn't work.</p>
  
  <p>I tried a few variations when I was defining relative path, but couldn't make any of them to work. I always get nullPointerException when this line <code>String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);</code> tries to execute. Am I doing something wrong with relative path or something else? I don't know if this is relevant but I am using <strong>Spring</strong>.</p>
  
  <pre><code>@RequestMapping(value = "/exportPhonebook.html", method = RequestMethod.POST)
  public void exportPhonebook(Model model, HttpServletResponse response) {
      try {
          setResponseHeaderPDF(response);
          Document document = new Document();
          ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
          PdfWriter pdfWriter = null;
          pdfWriter = PdfWriter.getInstance(document, baosPDF);
          PageNumbersEventHelper events = new PageNumbersEventHelper();
          pdfWriter.setPageEvent(events);
          document.open();
          addMetaData(document);
          addTitlePage(document);
          String relativeWebPath = "/img/image.png";
          String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);
          Image image1 = Image.getInstance(absoluteDiskPath);
          document.add(image1);
          addContent(document);
          document.close();
          pdfWriter.close();
          OutputStream os = response.getOutputStream();
          baosPDF.writeTo(os);
          os.flush();
          os.close();
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>This is how I used it in jsp:</p>
  
  <pre><code>&lt;input type="hidden" value="&lt;%=getServletContext().getRealPath("/") %&gt;" name="path"&gt;
  </code></pre>
  
  <p>I pass this to controller and add relative path to this path.</p>
  
  <pre><code>path = path + "img\\image.png";
  Image image = Image.getInstance(path);
  </code></pre>
  
  <p>THis works just fine. 
  I don't understand why this doesn't work in my controller.</p>