Help--Null Pointer Exception

S

Steve R. Burrus

Yes, I need some help/assistance with always getting this particular
servlet error exception when I try to execute an image servlet! Can
someone help me? (And look below the error msg. for the file itself)
java.lang.NullPointerException
at com.steven.burris.LaundryGirl.doGet(LaundryGirl.java:24)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:252)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:173)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:214)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:178)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:126)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j

..........."<<

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

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

ServletContext sc = getServletContext ();
InputStream is = sc.getResourceAsStream("/MyBeautifulGirl.jpg");

int read = 0;
byte[] bytes = new byte[1024];

OutputStream os = resp.getOutputStream();
while( ( read = is.read(bytes)) != -1){
os.write(bytes, 0, read);
}
os.flush();
os.close();
}"<<
 
R

Rhino

Steve R. Burrus said:
Yes, I need some help/assistance with always getting this particular
servlet error exception when I try to execute an image servlet! Can
someone help me? (And look below the error msg. for the file itself)
java.lang.NullPointerException
at com.steven.burris.LaundryGirl.doGet(LaundryGirl.java:24)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:252)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:173)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:214)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:178)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:126)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j

.........."<<

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

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

ServletContext sc = getServletContext ();
InputStream is = sc.getResourceAsStream("/MyBeautifulGirl.jpg");

int read = 0;
byte[] bytes = new byte[1024];

OutputStream os = resp.getOutputStream();
while( ( read = is.read(bytes)) != -1){
os.write(bytes, 0, read);
}
os.flush();
os.close();
}"<<

Uhh, which line in 'LaundryGirl' is line 24? I tried counting down 24 lines
from the top of the listing but that is the 'os.close();' line and it seems
unlikely you'd get a NullPointerException there when the os.flush() just
before it apparently worked. Something tells me that there are some extra
blank lines or some wrapping throwing off the count....

By the way, I don't know if this is significant but I noticed that in your
package name, your last name is spelled 'Burris' but in your email ID, it is
'Burrus'. I'm not sure which of those is incorrect but if you've made a
similar typo in your code, perhaps something isn't getting found because
you've specified the wrong directory for it or something like that....

Rhino
 
D

Dotty

Rhino said:
Steve R. Burrus said:
Yes, I need some help/assistance with always getting this particular
servlet error exception when I try to execute an image servlet! Can
someone help me? (And look below the error msg. for the file itself)
"SEVERE: Servlet.service() for servlet MyLuciousGirl threw exception
java.lang.NullPointerException
at com.steven.burris.LaundryGirl.doGet(LaundryGirl.java:24)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:252)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:173)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:214)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:178)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:126)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j

.........."<<
"package com.steven.burris;

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

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

ServletContext sc = getServletContext ();
InputStream is = sc.getResourceAsStream("/MyBeautifulGirl.jpg");

int read = 0;
byte[] bytes = new byte[1024];

OutputStream os = resp.getOutputStream();
while( ( read = is.read(bytes)) != -1){
os.write(bytes, 0, read);
}
os.flush();
os.close();
}"<<

Uhh, which line in 'LaundryGirl' is line 24? I tried counting down 24 lines
from the top of the listing but that is the 'os.close();' line and it seems
unlikely you'd get a NullPointerException there when the os.flush() just
before it apparently worked. Something tells me that there are some extra
blank lines or some wrapping throwing off the count....

If 'os' is null then 'os.close()' will give the NullPointerException
message.
 
T

Tony Morris

Steve R. Burrus said:
Yes, I need some help/assistance with always getting this particular
servlet error exception when I try to execute an image servlet! Can
someone help me? (And look below the error msg. for the file itself)
java.lang.NullPointerException
at com.steven.burris.LaundryGirl.doGet(LaundryGirl.java:24)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:252)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:173)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:214)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:178)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:126)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j

.........."<<

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

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

ServletContext sc = getServletContext ();
InputStream is = sc.getResourceAsStream("/MyBeautifulGirl.jpg");

int read = 0;
byte[] bytes = new byte[1024];

OutputStream os = resp.getOutputStream();
while( ( read = is.read(bytes)) != -1){
os.write(bytes, 0, read);
}
os.flush();
os.close();
}"<<

http://www.xdweb.net/~dibblego/java/NPE
 
T

Tilman Bohn

In message <[email protected]>,
Dotty wrote on Sun, 27 Feb 2005 23:03:46 -0600:

[...]
If 'os' is null then 'os.close()' will give the NullPointerException
message.

No. Because the os.flush() right before it would have thrown that
same exception already, os.close() would never execute.
 
T

Tilman Bohn

java.lang.NullPointerException
at com.steven.burris.LaundryGirl.doGet(LaundryGirl.java:24) [...]

ServletContext sc = getServletContext ();
InputStream is = sc.getResourceAsStream("/MyBeautifulGirl.jpg");

You should check if you really got an InputStream...
int read = 0;
byte[] bytes = new byte[1024];

OutputStream os = resp.getOutputStream();
while( ( read = is.read(bytes)) != -1){

I suspect this is line 24. Is it?
 
D

Dotty

Tilman Bohn said:
In message <[email protected]>,
Dotty wrote on Sun, 27 Feb 2005 23:03:46 -0600:

[...]
If 'os' is null then 'os.close()' will give the NullPointerException
message.

No. Because the os.flush() right before it would have thrown that
same exception already, os.close() would never execute.

Ok Mr. picky, if 'os' is null then 'os.write()' would fail before the
'os.flush()'
 
T

Tilman Bohn

In message <[email protected]>,
Dotty wrote on Mon, 28 Feb 2005 14:33:31 -0600:

[...]
OutputStream os = resp.getOutputStream();
while( ( read = is.read(bytes)) != -1){
os.write(bytes, 0, read);
}
os.flush();
os.close();
}"<< [...]
If 'os' is null then 'os.close()' will give the NullPointerException
message.

No. Because the os.flush() right before it would have thrown that
same exception already, os.close() would never execute.

Ok Mr. picky, if 'os' is null then 'os.write()' would fail before the
'os.flush()'

Yeah, but only if it even gets there, which is not the case if the
InputStream didn't have any data. ;-)
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top