Help--Null Pointer Exception

Discussion in 'Java' started by Steve R. Burrus, Feb 28, 2005.

  1. 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();
    }"<<
     
    Steve R. Burrus, Feb 28, 2005
    #1
    1. Advertising

  2. Steve R. Burrus

    Rhino Guest

    "Steve R. Burrus" <> wrote in message
    news:VfwUd.19985$...
    > 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....

    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
     
    Rhino, Feb 28, 2005
    #2
    1. Advertising

  3. Steve R. Burrus

    Dotty Guest

    "Rhino" <> wrote in message
    news:1lxUd.4828$...
    >
    > "Steve R. Burrus" <> wrote in message
    > news:VfwUd.19985$...
    > > 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.
     
    Dotty, Feb 28, 2005
    #3
  4. Steve R. Burrus

    Tony Morris Guest

    "Steve R. Burrus" <> wrote in message
    news:VfwUd.19985$...
    > 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();
    > }"<<


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

    --
    Tony Morris
    http://xdweb.net/~dibblego/
     
    Tony Morris, Feb 28, 2005
    #4
  5. Steve R. Burrus

    Tilman Bohn Guest

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

    [...]
    >> > 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.


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

    --
    Cheers, Tilman

    `Boy, life takes a long time to live...' -- Steven Wright
     
    Tilman Bohn, Feb 28, 2005
    #5
  6. Steve R. Burrus

    Tilman Bohn Guest

    In message <VfwUd.19985$>,
    Steve R. Burrus wrote on Mon, 28 Feb 2005 03:31:01 GMT:

    > >>"SEVERE: Servlet.service() for servlet MyLuciousGirl threw exception

    > 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?

    --
    Cheers, Tilman

    `Boy, life takes a long time to live...' -- Steven Wright
     
    Tilman Bohn, Feb 28, 2005
    #6
  7. Steve R. Burrus

    Dotty Guest

    "Tilman Bohn" <> wrote in message
    news:...
    > In message <>,
    > Dotty wrote on Sun, 27 Feb 2005 23:03:46 -0600:
    >
    > [...]
    > >> > 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.

    >
    > 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()'
     
    Dotty, Feb 28, 2005
    #7
  8. Steve R. Burrus

    Tilman Bohn Guest

    In message <>,
    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. ;-)

    --
    Cheers, Tilman

    `Boy, life takes a long time to live...' -- Steven Wright
     
    Tilman Bohn, Feb 28, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    26,938
    Mike Schilling
    Mar 29, 2006
  2. Ken
    Replies:
    69
    Views:
    12,588
    Michael D. Borghardt
    Jul 31, 2004
  3. Ken
    Replies:
    5
    Views:
    2,553
    Victor Bazarov
    Jul 24, 2004
  4. aneuryzma
    Replies:
    3
    Views:
    743
    Jim Langston
    Jun 16, 2008
  5. Christopher
    Replies:
    4
    Views:
    454
    Ruben Safir
    Jul 9, 2011
Loading...

Share This Page