Try this...

   try {
     if (request.getParameter("test").equals("blah")) {
       // do something
     }
     else {
       // do something else
     }
   }
   catch (NullPointerException e) {
     // do something with the exception
   }


Please respond to A mailing list about Java Server Pages specification and
      reference <[EMAIL PROTECTED]>

To:   [EMAIL PROTECTED]
cc:
Subject:  Re: isset()




ok, I got 2 responses voting for checking if the cariale is equal to null
or use
the equals() function... maybe it's something wierd wih Resin, but if I say

if (request.getParameter("test")==null)

or

if (request.getParameter("test").equals("blah"))

and test was never passed instead of the conditional just returning false I
get a
NullPointerException.

-Daniel

christopher brown wrote:

> isset() is generally useful in PHP because it's a loosely-typed language
> that doesn't require variable declarations.  In Java, you can avoid a lot
of
> these issues because the compiler picks up situations where you try to
use a
> variable without declaring it beforehand, or if it's likely to have been
> unassigned a value.  For example:
>
> String s;
> if (some-conditional-test) s = "hello";
> System.out.println(s);
>
> ...should be picked up by the compiler.
>
> In a lot of other cases, you can often avoid returning null by returning
> empty arrays or collections in Java.  In many others, you can also
carefully
> arrange expressions, such as:
>
> if ("something".equals(request.getParameter("abc"))) ...;
>
> This is good practice in any case, as even if there's no such parameter
> "abc", you'll always avoid a NullPointerException.
>
> Hint (OT): with PHP, or other loosely-typed languages such as JavaScript,
> try forcing types correctly by using literals before variables in
> expressions.  Instead of:
>   if (x > 0) ...
> Write:
>   if (0 < x)
>
> Back on topic, in all other cases, what's wrong with using try...catch to
> handle NullPointerExceptions ?  It's arguably much cleaner than mixing in
> "if (isset($x))" throughout your code.  You can arrange the normal flow
of
> execution, assuming everything goes well, within a try...catch block.  If
> exceptionnally something is null, handle this elsewhere.  And if that
> doesn't suit, just try:
>
> if (null == mavariable) ...;
>
> Hope this helps!
> -Christopher Brown
>
> ----- Original Message -----
> From: "Daniel Lynn" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Thursday, March 15, 2001 6:23 AM
> Subject: isset()
>
> > ok, in PHP there is a command called isset(). It returns a boolean to
> > tell you if a certain variable was passed to the program or not. (Helps
> > eliminate those nullpointer exceptions without putting everything in
> > try/catch statements) Now, I was looking for one in JSP and I'm having
a
> > little trouble. I think it may just be I don't know what I'm looking
> > for, so if anyone would be so kind as to let me know the name of the
> > function if it exists it'd be MUCH appreciated. Thank you in advance.
> >
> > -Daniel
> >
> >
>
===========================================================================
> > To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
> JSP-INTEREST".
> > For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
> DIGEST".
> > Some relevant FAQs on JSP/Servlets can be found at:
> >
> >  http://java.sun.com/products/jsp/faq.html
> >  http://www.esperanto.org.nz/jsp/jspfaq.html
> >  http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
> >  http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
>
>
===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
> For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
DIGEST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
>  http://java.sun.com/products/jsp/faq.html
>  http://www.esperanto.org.nz/jsp/jspfaq.html
>  http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
>  http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to