title: Cannot download a file using HttpServletResponse
body: <p>I want to download a MS Word 2003 document that I am creating with the content <code>myString</code> in it.
  I have used the following code:</p>
  
  <pre><code>BufferedReader reader = new BufferedReader(new FileReader(templatePathFilename));
  HttpServletResponse response = new MyHttpServletResponse();
  response.setContentType ("application/msword"); 
  response.setHeader ("Content-Disposition", "attachment; filename=\""+outgoingFileName); 
  ServletOutputStream myOut = response.getOutputStream();
  myOut.write(myString.getBytes());
  myOut.flush();
  myOut.close();
  </code></pre>
  
  <p>But the line <code>myOut.write(myString.getBytes())</code> gives a NullPointerException.
  <code>MyHttpServletResponse</code> is a class that eclipse has produced by default when I quick fix the error generated. Do I need to modify that class?
  Can anyone please help!</p>
  
  <p>EDIT: The actual code i am working on is as below:</p>
  
  <pre><code>BufferedReader reader = new BufferedReader(new FileReader(templatePathFilename));
  String outgoingFileName = outputPathFilename;
  response.setContentType("application/msword"); 
  response.setHeader("Content-Disposition", "attachment; filename="+outgoingFileName);
  OutputStream myOut = response.getOutputStream();
  try {
  String thisLine;
  while ((thisLine = reader.readLine()) != null) {
      if(thisLine.contains("##"))
      {
      for (java.util.Enumeration e = FrontSheetMap.keys(); e.hasMoreElements();{
       String name = (String) e.nextElement();
       String value = FrontSheetMap.get(name).toString();
       thisLine= thisLine.replaceAll("##" + name.toUpperCase() + "##", value);
      }
     }
     myOut.write(thisLine);
     myOut.write("\n");
    }
    myOut.flush();
  }catch(Exception e){}
  </code></pre>
  
  <p>The while loop replaces the placeholders in the input file with required values and the new content in <code>thisLine</code> is to be written on the output file. I need a download option which pops up on clicking the link which executes this code.</p>