Re. Null Pointer Exception.

Discussion in 'Java' started by Steve R. Burrus, Mar 29, 2005.

  1. I know that SOME of you think that I am being "crazy" and foolhardy by still
    posting my problem about getting that "java.lang.NullPointerException"
    almost every time that I try/attempt to view a servlet image in my browser,
    but I swear, I have looked OVER AND OVER at the lines in the servlet file
    which I suspect could be giving me this server error, but I honestly could
    not find the problem! Truth In Representation : I HAVE been able to view
    just a few of the images successfully, but I really don't know why. Well,
    with my apologies to those of you who don't feel like I need again the
    servlet code, I humbly re-submit it to you :

    package com.laundry.gal;

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

    public class LuvThisGal extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse
    response) throws
    IOException,ServletException {
    response.setContentType("image/jpg");


    ServletContext ctx = getServletContext();
    InputStream is = ctx.getResourceAsStream("/GirlAtSink.jpeg");

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

    OutputStream os = response.getOutputStream();

    while((read = is.read(bytes)) != -1) {
    os.write(bytes, 0, read);
    response.sendError(500,"Hey! Your Servlet Developed A" +
    "NullPointerException.");

    }
    os.flush();
    os.close();

    }
    }
     
    Steve R. Burrus, Mar 29, 2005
    #1
    1. Advertising

  2. Steve R. Burrus

    kjc Guest

    Steve R. Burrus wrote:
    > I know that SOME of you think that I am being "crazy" and foolhardy by still
    > posting my problem about getting that "java.lang.NullPointerException"
    > almost every time that I try/attempt to view a servlet image in my browser,
    > but I swear, I have looked OVER AND OVER at the lines in the servlet file
    > which I suspect could be giving me this server error, but I honestly could
    > not find the problem! Truth In Representation : I HAVE been able to view
    > just a few of the images successfully, but I really don't know why. Well,
    > with my apologies to those of you who don't feel like I need again the
    > servlet code, I humbly re-submit it to you :
    >
    > package com.laundry.gal;
    >
    > import java.io.*;
    > import javax.servlet.*;
    > import javax.servlet.http.*;
    >
    > public class LuvThisGal extends HttpServlet {
    > public void doGet(HttpServletRequest request, HttpServletResponse
    > response) throws
    > IOException,ServletException {
    > response.setContentType("image/jpg");
    >
    >
    > ServletContext ctx = getServletContext();
    > InputStream is = ctx.getResourceAsStream("/GirlAtSink.jpeg");
    >
    > int read = 0;
    > byte[] bytes = new byte[1024];
    >
    > OutputStream os = response.getOutputStream();
    >
    > while((read = is.read(bytes)) != -1) {
    > os.write(bytes, 0, read);
    > response.sendError(500,"Hey! Your Servlet Developed A" +
    > "NullPointerException.");
    >
    > }
    > os.flush();
    > os.close();
    >
    > }
    > }
    >
    >
    >

    first put your code inside a try catch block.
    Second, in your catch block, print a stack trace.
    That will tell you exactly where the problem is.
     
    kjc, Mar 29, 2005
    #2
    1. Advertising

  3. Steve R. Burrus

    Guest

    I have a question about your code. I really don't much about servlets,
    but I'm wondering about the call to response.sendError inside the while
    loop. Does this cause the NullPointerException to be displayed after
    you write the first array of bytes?
     
    , Mar 29, 2005
    #3
  4. Steve R. Burrus wrote:
    > I know that SOME of you think that I am being "crazy" and
    > foolhardy by still posting my problem about getting that
    > "java.lang.NullPointerException" almost every time that
    > I try/attempt to view a servlet image in my browser, but
    > I swear, I have looked OVER AND OVER at the lines in the
    > servlet file which I suspect could be giving me this
    > server error, but I honestly could not find the problem!
    > Truth In Representation : I HAVE been able to view just a
    > few of the images successfully, but I really don't know
    > why. Well, with my apologies to those of you who don't
    > feel like I need again the servlet code, I humbly
    > re-submit it to you :


    The real problem is that you keep reposting this, with
    different subject lines, without any indication that you
    have read the numerous replies, or providing the additional
    information they indicated was needed. "It's still about the
    NPE." was a bit better than this one, because you included
    the stack trace. However, I didn't see a reply to e.g.:

    > Almost there. Now you just need to tell us which line is
    > 22. (If the package statement were the first line, 22
    > would consist of just a closing brace, which seems
    > unlikely.)


    (From Mike Schilling's reply in that thread.)

    I have some general advice on how to debug at
    http://home.earthlink.net/~patricia_shanahan/debug/. I do
    not recommend simply looking over the lines unless that
    works very quickly. You need to start a logical, structured
    debug effort.

    The very first step, as you have already been told, is to
    identify the line at which the exception happened. We don't
    know which line it is. You should.

    Once you have done that, list the pointers that are
    dereferenced on that line. One of them is null when the NPE
    is thrown. If there is more than one dereferenced pointer on
    the line, use a debugger or println to find out which is
    null on failure.

    Once you have identified the null pointer, you need to
    decide what to do about it.

    Patricia
     
    Patricia Shanahan, Mar 29, 2005
    #4
  5. Steve R. Burrus

    Anton Spaans Guest

    Re: Maybe this is the issue:

    "Steve R. Burrus" <> wrote in message
    news:G1j2e.3980$...
    > I know that SOME of you think that I am being "crazy" and foolhardy by

    still
    > posting my problem about getting that "java.lang.NullPointerException"
    > almost every time that I try/attempt to view a servlet image in my

    browser,
    > but I swear, I have looked OVER AND OVER at the lines in the servlet file
    > which I suspect could be giving me this server error, but I honestly could
    > not find the problem! Truth In Representation : I HAVE been able to view
    > just a few of the images successfully, but I really don't know why. Well,
    > with my apologies to those of you who don't feel like I need again the
    > servlet code, I humbly re-submit it to you :
    >
    > package com.laundry.gal;
    >
    > import java.io.*;
    > import javax.servlet.*;
    > import javax.servlet.http.*;
    >
    > public class LuvThisGal extends HttpServlet {
    > public void doGet(HttpServletRequest request, HttpServletResponse
    > response) throws
    > IOException,ServletException {
    > response.setContentType("image/jpg");
    >
    >
    > ServletContext ctx = getServletContext();
    > InputStream is = ctx.getResourceAsStream("/GirlAtSink.jpeg");
    >
    > int read = 0;
    > byte[] bytes = new byte[1024];
    >
    > OutputStream os = response.getOutputStream();
    >
    > while((read = is.read(bytes)) != -1) {
    > os.write(bytes, 0, read);
    > response.sendError(500,"Hey! Your Servlet Developed A" +
    > "NullPointerException.");
    >
    > }
    > os.flush();
    > os.close();
    >
    > }
    > }
    >
    >


    The only variable that can possibly be null, as far as i can see right now,
    is the 'InputStream is'
    The 'is = ctx.getResourceAsStream("/...."); ' statement can return null. See
    the java-doc:
    =============================================
    public java.io.InputStream getResourceAsStream(java.lang.String path)
    - Returns the resource located at the named path as an InputStream object.
    - The data in the InputStream can be of any type or length. The path must be
    specified according to the rules given in getResource. ***This method
    returns null if no resource exists at the specified path.***

    - Meta-information such as content length and content type that is available
    via getResource method is lost when using this method.

    - The servlet container must implement the URL handlers and URLConnection
    objects necessary to access the resource.

    - This method is different from java.lang.Class.getResourceAsStream, which
    uses a class loader. This method allows servlet containers to make a
    resource available to a servlet from any location, without using a class
    loader.

    Parameters:
    name - a String specifying the path to the resource
    Returns:
    the InputStream returned to the servlet, or null if no resource exists at
    the specified path
    =============================================

    Check if the resource "/GirlAtSink.jpeg" exists. In short, test if 'is ==
    null'!!!
    -- Anton Spaans.
     
    Anton Spaans, Mar 29, 2005
    #5
  6. Steve R. Burrus

    P.Hill Guest

    Steve R. Burrus wrote:
    > I know that SOME of you think that I am being "crazy" and foolhardy by still
    > posting my problem about getting that "java.lang.NullPointerException"


    Is it possible you have not read any of the replies on the newsgroup
    and you are waiting for an e-mail?

    Read the replys in the news group, please.

    -Paul
     
    P.Hill, Mar 30, 2005
    #6
  7. Can anyone explain to me, to my satisfaction, why it is that I only get the
    "java.lang.NullPointerException" on SOME of the images which I try/attempt
    to view in my web browser, but on maybe 1 or 2 others I can view
    successfully!!!

    And I need someone to please explain how to use the FileInputStream (and
    the FileOutputStream class also?) with the "getRealPath( )" Java method to
    enable me to finally see ALL of the images that I would like to see.

    And also, I was told by somebody that I should put the various images into
    my Tomcat's (I have the version 5.5.7 of it incidentally) WEB-INF\classes
    folder or directory. Is this the right thing to do or not? And how do I go
    about making a "Library" pertaining to these images that I was told goes
    into the WEB-INF\lib folder anyway? If I have these images in the main
    servlet context folder for the web application, are they said to be within
    the web app's scope ot not? Frankly people, I am going crazy and downright
    out of my mind with sheer frustration because I still haven't done this
    right, or at least to my satisfaction at all!!! I will be friends with
    someone in our group who can help me with this forever. One more question :
    Do I still have to use the ServletContext class when I use the
    getRealPath( ) method?
     
    Steve R. Burrus, Mar 31, 2005
    #7
  8. Steve R. Burrus wrote:

    > Can anyone explain to me, to my satisfaction, why it is
    > that I only get the "java.lang.NullPointerException" on
    > SOME of the images which I try/attempt to view in my web
    > browser, but on maybe 1 or 2 others I can view
    > successfully!!!


    I don't know, but I have a general remark on debug technique
    that might help resolve this:

    **** You don't need to do it all at once. ****

    Nobody expects to go from no code to the complete, final
    program for anything non trivial in one step. Programmers
    know they are making progress as long as the program becomes
    more and more similar to the program they want. Progress can
    be passing more test cases, having tests for more of the
    required features, nailing down the design for a critical
    part of the program, anything that brings the program closer
    to the requirements.

    In the same way, beyond the most trivial cases, debug is
    best treated as a stepwise refinement process. It is often
    impossible, or at least unnecessarily difficult, to jump
    straight from external symptom to root cause. Going from
    "There is a NullPointerException" to "This line sometimes
    throws a NullPointerException" is progress. From there to
    "When the program fails, this variable is null on arrival at
    the line, and gets dereferenced." is another step.

    Most of the people responding in your NullPointerException
    threads seem to be taking the stepwise refinement view. They
    ask questions, or propose theories to test. They expect you
    to follow up by finding out the answers to the questions or
    testing the theories, and posting additional information. If
    you did that, you would get more questions and/or theories
    to test, until someone understood the root cause, and the
    conversation would switch to a discussion of solutions.

    Meanwhile, you seem to expect to leap straight from "This
    block of code throws a NullPointerException" to the root
    cause, and what to do about it, without going through any
    intermediate steps. If you were going to get lucky, and have
    someone make the leap for you, it would probably have
    happened about three weeks ago. Surely by now it's time to
    give the stepwise refinement approach a chance?

    Patricia
     
    Patricia Shanahan, Mar 31, 2005
    #8
  9. "Steve R. Burrus" <> wrote in message
    news:G1j2e.3980$...

    <<snip>>
    I am now re-posting only a part of my post from late last nite, and I would
    really like to have some specific answers to these particular questions.
    Thanx.

    And I need someone to please explain how to use the FileInputStream (and
    the FileOutputStream class also?) with the "getRealPath( )" Java method to
    enable me to finally see ALL of the images that I would like to see.

    And also, I was told by somebody that I should put the various images into
    my Tomcat's (I have the version 5.5.7 of it incidentally) WEB-INF\classes
    folder or directory. Is this the right thing to do or not? And how do I go
    about making a "Library" pertaining to these images that I was told goes
    into the WEB-INF\lib folder anyway? If I have these images in the main
    servlet context folder for the web application, are they said to be within
    the web app's scope ot not?
     
    Steve R. Burrus, Mar 31, 2005
    #9
  10. Re:Null Pointer Exception.

    Well, I just wanted to tell everybody/everyone that I seemed to get it
    right, finally, with being able to view all of the various images in my web
    browser with this 1 line of code : >>"FileInputStream fis = new
    FileInputStream(ctx.getRealPath([name of image file]));"<< Sorry to be such
    a "bother" to some of you people.


    "Steve R. Burrus" <> wrote in message
    news:xUZ2e.9930$...
    > <<snip>>
     
    Steve R. Burrus, Mar 31, 2005
    #10
  11. Steve R. Burrus

    Anton Spaans Guest

    Re: Your solution may not work in WAR deployed webapps.

    "Steve R. Burrus" <> wrote in message
    news:2h_2e.9934$...
    > Well, I just wanted to tell everybody/everyone that I seemed to get it
    > right, finally, with being able to view all of the various images in my

    web
    > browser with this 1 line of code : >>"FileInputStream fis = new
    > FileInputStream(ctx.getRealPath([name of image file]));"<< Sorry to be

    such
    > a "bother" to some of you people.
    >
    >
    > "Steve R. Burrus" <> wrote in message
    > news:xUZ2e.9930$...
    > > <<snip>>

    >
    >


    Your solution will work in web-applications that are deployed as a
    file-directory hierarchy. If the web-app is deployed in a WAR file (and the
    WAR file is not being 'exploded' by the web-server/web-container), then
    getRealPath() will return null!
    The ctx.getResourceAsStream(String pPath) will work in both situations. Just
    make sure that the pPath is correct.

    ctx.getResourceAsStream("/image.gif") will return the inputstream for the
    image file "<web-root>/image.gif".
    ctx.getResourceAsStream("/somedir/image.gif") will return the inputstream
    for the image file "<web-root>/somedir/image.gif".
    Do not put them in or below the "<web-root>/WEB-INF" directory. The
    WEB-INF/classes and WEB-INF/lib are only know are 'root'-paths to your
    web-server's classloader!

    BTW: you were never a "bother" :=)

    -- Anton.
     
    Anton Spaans, Mar 31, 2005
    #11
    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:
    27,232
    Mike Schilling
    Mar 29, 2006
  2. Ken
    Replies:
    69
    Views:
    12,633
    Michael D. Borghardt
    Jul 31, 2004
  3. Ken
    Replies:
    5
    Views:
    2,567
    Victor Bazarov
    Jul 24, 2004
  4. aneuryzma
    Replies:
    3
    Views:
    761
    Jim Langston
    Jun 16, 2008
  5. Christopher
    Replies:
    4
    Views:
    458
    Ruben Safir
    Jul 9, 2011
Loading...

Share This Page