title: Null pointer exception with getServletContext()?
body: <p>I'm a rookie programmer developing my first dynamic web application on eclipse for tomcat.  It communicates with a mySQL database and modifies some template files and saves them as new ones. The app needs to be portable. I know about absolute and relative paths, but am super confused about deciding where to put the extra resources and what to put in my sql database path so they can be accessed by the .war file. On my PC I'm able to connect to mySQL @ <code>jdbc:mysql//localhost:3306/dbname</code>. But I'm sure this url won't work on any other device.. </p>
  
  <p>How do I make everything portable? In what locations should I copy my templates to create a .war application which can access them using relative paths!</p>
  
  <p>I found some help for the latter part <a href="https://stackoverflow.com/questions/2455474/in-servlet-web-app-how-do-i-know-the-relative-path">here</a> but I think it requires putting the files in the tomcat directory which in my opinion won't be bundled with the app.</p>
  
  <p>Edit: Here's my code</p>
  
  <p>Web form</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
      pageEncoding="UTF-8"%&gt;       
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
  &lt;title&gt;Insert title here&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form action="test"&gt;
  &lt;input type="text" name="name"&gt; &lt;/input&gt;
  &lt;/form&gt;
  &lt;button type="submit"&gt;Create&lt;/button&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Servlet:</p>
  
  <pre><code>package rtest;
  
  import java.io.File;
  import java.io.IOException;
  import java.nio.file.Files;
  
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  /**
   * Servlet implementation class Testservlet
   */
  @WebServlet(description = "testing", urlPatterns = { "/test" })
  public class Testservlet extends HttpServlet {
      private static final long serialVersionUID = 1L;
  
      /**
       * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          // TODO Auto-generated method stub
          File source = new File(getServletContext().getRealPath("templates/fiel1.docx"));
          File dest = new File(getServletContext().getRealPath("templates/FF.docx"));
  
          try{
              Files.copy(source.toPath(), dest.toPath());
          }catch(Exception e)
          {
              e.printStackTrace();
  
          }
      }
  
  }
  </code></pre>
  
  <p>Edit: I'm getting a null pointer exception although I have the templates folder in under web content!</p>