Sorry Martin -- won't work the way you did it.  Testing for null will
not throw an NPE.  That's how we avoid an NPE all together.

However if you attempt to access a method on a null object reference, it
will throw an NPE.  Example:

try {
  javax.sql.DataSource dbds = null ;
  dbds.getConnection() ; // This will definitely throw an NPE
} catch ( java.lang.NullPointerException npe ) {
  // Do something else because we couldn't get a connection
}

Welcome to java programming 101.

Additionally some methods in various APIs will throw a NPE if you
provide a null value on a parameter.

--David

Martin Gainty wrote:

>the instant you execute code where a variable is NULL you will throw
NullPointerException
>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");
>}
>
>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]



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