Results 1 to 4 of 4
  1. #1
    Zeus is offline Member
    Join Date
    Jun 2010
    Posts
    18
    Rep Power
    0

    Default DownLoad A File "NullPointerException"

    i get java.lang.NullPointerException.... the code is below


    public void service(
    HttpServletRequest request,
    HttpServletResponse response
    ) throws IOException{
    response.setContentType("text/plain");
    response.setHeader("Content-Disposition",
    "attachment;filename=sample.txt");
    ServletContext ctx = getServletContext();
    InputStream is = ctx.getResourceAsStream("sample.txt");

    int read=0;
    byte[] bytes = new byte[BYTES_DOWNLOAD];
    OutputStream os = response.getOutputStream();

    while((read = is.read(bytes))!= -1){
    os.write(bytes, 0, read);
    }
    os.flush();
    os.close();
    }
    can any one help me to fix the exception

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Use code tags to keep the formatting of your code. Unformatted code is really hard to read.

    Also, tell us the full exception (stack trace and all), as well as which line it is occurring on...not the line number, since we won't know where that is, but the actual line of code.

  3. #3
    mangstadt is offline Member
    Join Date
    Jun 2010
    Location
    USA
    Posts
    19
    Rep Power
    0

    Default

    ctx.getResourceAsStream() is probably returning null because it can't find the resource. Where is "sample.txt" located? I think you have to prefix the path with a "/":

    Java Code:
    InputStream is = ctx.getResourceAsStream("/sample.txt");

    Also, please add code tags.

  4. #4
    Zeus is offline Member
    Join Date
    Jun 2010
    Posts
    18
    Rep Power
    0

Similar Threads

  1. Replies: 3
    Last Post: 04-30-2010, 07:44 PM
  2. Replies: 5
    Last Post: 08-26-2009, 11:37 AM
  3. Replies: 6
    Last Post: 08-03-2009, 08:58 PM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  5. Replies: 1
    Last Post: 07-16-2007, 06:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •