title: How to write to a file from a web application in Tomcat 8?
body: <p>I have asked this question before and received a lot of answers within regard to how to fix the issue. However the issue still has not been repaired and I am at a loss when it comes to the right means to repair it. I am creating a java web application in Net beans using Tomcat 8 and Java EE 7. The application is a simple form application that takes a users information and writes it to a simple text file in the WEB-INF folder. However I have tried several ways to write to the text file and all but one throw an error the one that doesn't throw an error is the one that is supposed to be correct given the research and information I have found, and the great answers that I have gotten from everyone here. My problem is that none of them work. I have moved from model 1 architecture to model 2 architecture and the like and still haven't found repair. I am posting a link to my Git Repository with all of the code inside. Please if anyone can take a look at it and let me know what is going on that would be great.
  <a href="https://github.com/RichardDavy42/MailList3" rel="nofollow">My GitHub</a>!</p>
  
  <pre><code>//doPost Method from servlet
  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
      //get parameters from the form
      String firstName = request.getParameter("firstName");
      String lastName = request.getParameter("lastName");
      String email = request.getParameter("email");
  
      //get relative file name
      //ServletContext context = getServletContext();
      String path = getServletContext().getRealPath("/WEB-INF/my_new_text.txt");
  
       //add info to text file
       User user = new User(firstName, lastName, email);
       UserIO.add(user, path);
  
       //Store user object in a request object
       request.setAttribute("user", user);
  
       //forward request and response objects to JSP page
       String url = "/display_email_entry.jsp";
       RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
       dispatcher.forward(request, response);
  }
  
  //UserIO class for writing to file 
  package data;
  
  import business.User;
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.FileWriter;
  import java.io.IOException;
  import java.io.PrintWriter;
  import static java.rmi.server.LogStream.log;
  
  /**
   *
   * @author Richard Davy
   */
  public class UserIO {
      public static void add(User user, String filepath)throws IOException{
  
          FileOutputStream file = new FileOutputStream(filepath, true);
          String firstName = user.getFirstName();
          String lastName = user.getLastName();
          String email = user.getEmail();
          PrintWriter writer = new PrintWriter(file);
          try{
              writer.println(user.getEmail() + "|" + user.getFirstName() + "|" +     user.getLastName());
              System.out.print(email + "|" + firstName + "|" + lastName);
          }catch(Exception ex){
              System.out.println("Errors" + ex);
          }finally{
              writer.flush();        
              writer.close();
          }
      }
  }
  
  
  15-Jun-2014 22:24:12.321 SEVERE [354]   org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet   [AddToEmailList] in context with path [/MailList3] threw exception
   java.lang.NullPointerException
  at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:206)
  at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:104)
  at java.io.FileWriter.&lt;init&gt;(FileWriter.java:63)
  at data.UserIO.add(UserIO.java:22)
  at email.AddToEmailList.doPost(AddToEmailList.java:89)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:301)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
  at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
  at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:78)
  at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
  at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1033)
  at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:652)
  at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
  at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
  at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
  at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
  at java.lang.Thread.run(Thread.java:724)
  </code></pre>