Need help w. NullPointerException.

S

Steve R. Burrus

i find myself in need of some help/assistance for a servlet that compiles
okay, but when I try to execute it in my browser I always get a
"java.lang.NullPointerException" server error!!! The servlet, incidentally,
is one that is supposed to render a image. I have no idea of what the
problem is, but many thanx to whoever can solve this one for me. I am very
frustrated by this! (Here is the servlet code) :

package org.my.servlets;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class My_Image extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("image/jpg");

ServletContext con = getServletContext();
InputStream in = con.getResourceAsStream("/MyBeautifulGirl1.jpg");

int i = 0;
byte[] b = new byte[1024];

OutputStream os = resp.getOutputStream() ;
while(( i = in.read(b)) != -1){
os.write(b, 0, i);
}
os.flush();
os.close();
}
}
 
T

Tony Morris

Steve R. Burrus said:
i find myself in need of some help/assistance for a servlet that compiles
okay, but when I try to execute it in my browser I always get a
"java.lang.NullPointerException" server error!!! The servlet, incidentally,
is one that is supposed to render a image. I have no idea of what the
problem is, but many thanx to whoever can solve this one for me. I am very
frustrated by this! (Here is the servlet code) :

package org.my.servlets;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class My_Image extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("image/jpg");

ServletContext con = getServletContext();
InputStream in = con.getResourceAsStream("/MyBeautifulGirl1.jpg");

int i = 0;
byte[] b = new byte[1024];

OutputStream os = resp.getOutputStream() ;
while(( i = in.read(b)) != -1){
os.write(b, 0, i);
}
os.flush();
os.close();
}
}

http://www.xdweb.net/~dibblego/java/NPE
(minor fixes pending)
 
R

Ryan Stewart

Steve R. Burrus said:
i find myself in need of some help/assistance for a servlet that compiles okay,
but when I try to execute it in my browser I always get a
"java.lang.NullPointerException" server error!!! The servlet, incidentally, is
one that is supposed to render a image. I have no idea of what the problem is,
but many thanx to whoever can solve this one for me. I am very frustrated by
this! (Here is the servlet code) :
[...]
A stack trace and line numbers would be extremely helpful. From what you've
posted, my guess would be that your problem is here (that it can't find the
resource you've specified):
InputStream in = con.getResourceAsStream("/MyBeautifulGirl1.jpg");

and that the Exception is being thrown here:
while(( i = in.read(b)) != -1){
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top