Results 1 to 4 of 4
- 06-30-2010, 06:27 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 18
- Rep Power
- 0
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
- 06-30-2010, 09:11 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 07-01-2010, 05:28 PM #3
Member
- Join Date
- Jun 2010
- Location
- USA
- Posts
- 19
- Rep Power
- 0
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.
- 07-30-2010, 01:30 PM #4
Member
- Join Date
- Jun 2010
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
ERROR: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
By PFworth in forum New To JavaReplies: 3Last Post: 04-30-2010, 07:44 PM -
help debugging: exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
By freqrush in forum AWT / SwingReplies: 5Last Post: 08-26-2009, 11:37 AM -
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
By nikx_8_7 in forum AWT / SwingReplies: 6Last Post: 08-03-2009, 08:58 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM -
ArrayList: Exception in thread "main" java.lang.NullPointerException
By susan in forum New To JavaReplies: 1Last Post: 07-16-2007, 06:32 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks