You already have the answer (code you posted modified with the answer):


                          package coreservlets;

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

public class Image_File extends HttpServlet {
public void doGet( HttpServletRequest rq, HttpServletResponse rp ) throws ServletException, IOException {
      rp.setContentType( "image/jpeg" );

      ServletContext sc = getServletContext ();
      InputStream in = sc.getResourceAsStream("/Sexy_Laundry_Girl!.JPG");

      if ( in != null ) {
      int r = 0;
      byte[] by = new byte[4096];

      OutputStream os = rp.getOutputStream();
      while( ( r = in.read(by)) != -1) {
           // if (r in= null)
          os.write(by, 0, r);
     }
     os.flush();
     os.close() ;
     }
     else {
// Try something else -- maybe a known good image to take it's place.

     }
 }
}

--David

Steve Burrus wrote:
fellas how about getting back to my original problem please!! I simply wanted 
to see an image of something in a servlet. Now please refer back to my code 
which I have submitted to this group and on the line w. the while loop I ALWAYS 
have gotten the NPE. Any FRESH ideas about this possibly?


----- Original Message ----
From: Martin Gainty <[EMAIL PROTECTED]>
To: Tomcat Users List <users@tomcat.apache.org>
Sent: Monday, November 13, 2006 4:37:48 PM
Subject: Re: How do I ........?


Well bs chuck and laughing dave

I ran your exact example / deleted FunkyImage.jpg and got this on screen

java.lang.NullPointerException
    coreservlets.ImageFile.doGet(ImageFile.java:34)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

**********But wait a minute Im NOT supposed to get NullPointerException 
!!!!!***********

public class ImageFile extends HttpServlet
{
public void doGet( HttpServletRequest rq, HttpServletResponse rp ) throws  
ServletException, IOException
// public static void main(String[] args)
{
//     javax.servlet.http.HttpServletResponse rp;
//     javax.servlet.http.HttpServletRequest rq;
     ServletContext sc;

        rp.setContentType( "image/jpeg" );

        sc = getServletContext ();
        InputStream in = sc.getResourceAsStream("FunkyImage.JPG");

        int r = 0;
        byte[] by = new byte[4096];

        OutputStream os = rp.getOutputStream();

            if (in==null) System.out.println("This is when in == null");

        while( ( r = in.read(by)) != -1)
        {
            if (in==null) System.out.println("This is when in == null");
           os.write(by, 0, r);
        }
        os.flush();
        os.close() ;
   } //DoGet
} //ImageFile

How is it that (in==null) didnt evaluate true ?

Comments?
M-
This e-mail communication and any attachments may contain confidential and privileged information for the use of the designated recipients named above. If you are not the intended recipient, you are hereby notified that you have received this communication in error and that any review, disclosure, dissemination, distribution or copying of it or its contents ----- Original Message ----- From: "david.delbecq" <[EMAIL PROTECTED]>
To: "Tomcat Users List" <users@tomcat.apache.org>
Sent: Monday, November 13, 2006 4:03 PM
Subject: Re: How do I ........?


o.O

.............
.............
(not laughing, not laughing, not laughing)

this is going to be my funniest evening in the week, and yet it still
monday!


Martin Gainty a écrit :
the instant you execute code where a variable is NULL you will  throw
NullPointerException

Change your books! Here are the conditions in which
NullPointerExceptions can be thrown (can be found in sun's javadocs)

Thrown when an application attempts to use |null| in a case where an
object is required. These include:

   * Calling the instance method of a |null| object.
   * Accessing or modifying the field of a |null| object.
   * Taking the length of |null| as if it were an array.
   * Accessing or modifying the slots of |null| as if it were an array.
   * Throwing |null| as if it were a |Throwable| value.

Applications should throw instances of this class to indicate other
illegal uses of the |null| object.


(notice the use of the term *required*)

if you want robust code you are better off wrapping in try catch block
as in
try
{
 if( in == null) System.out.println("This statement will never be
executed as NullPointerException will be thrown");
}
catch(NullPointerException npe)
{
  System.out.println("variable in has thrown NullPointerException so
something about that here");
}
Did you actually *tried* that code?

This e-mail communication and any attachments may contain confidential
and privileged information for the use of the
designated recipients named above. If you are not the intended
recipient, you are hereby notified that you have received
this communication in error and that any review, disclosure,
dissemination, distribution or copying of it or its
contents
----- Original Message -----
From: "Christopher Schultz" <[EMAIL PROTECTED]>
To: "Tomcat Users List" <users@tomcat.apache.org>
Sent: Monday, November 13, 2006 2:57 PM
Subject: Re: How do I ........?


Steve,

Steve R Burrus wrote:
hi chris this is steve Burrus and the line of code in question is the
while loop : "while( ( r = in.read(by)) != -1) {" the server error
for a
long time has always pointed to that line of code creating the NPE.
Yup, that's the first time you use the InputStream after attempting to
load your file. It's pretty simple to check for null: just use the code
I suggested in my last message:

Correct me if I'm wrong, but this generally checks for null:

if(null == in)
  // handle the null condition
Now, it's up to you what you want to do in the event of a NULL image.
You could either return a "404 Not Found" error status, or return a
known good image that actually says "ERROR" on it.

-chris

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
Mouhahahahaha. (sorry, couldn't hold it)


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to