Solved

Why is page erroring?

Posted on 2004-03-24
3
172 Views
Last Modified: 2011-04-14
This works:
////////////////////////////////////////////////////////////////////

if(strReferer == null){}

// is ok to continue
if (strReferer.indexOf("brocade.com") != -1) {}

///////////////////////////////////////////////////////////////////

This errors

////////////////////////////////////////////////////////////////////


// is ok to continue
if ((strReferer.indexOf("brocade.com") != -1)&&(strReferer != null)) {}

////////////////////////////////////////////////////////////////////
why?
0
Comment
Question by:894359
  • 2
3 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 10672580
short circuit checks in the wrong order:

if ((strReferer != null) && (strReferer.indexOf("brocade.com") != -1) {}

0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 500 total points
ID: 10672587
oops:

if ((strReferer != null) && (strReferer.indexOf("brocade.com") != -1)) {}

basically, the && operator is a short-circuit operator... what that means is it first checks the first condition (strReferer != null) if this is false, the conditional automatically returns false (b/c we don't care what the second condition is.

in your example... if strReferer is null and you try strReferer.indexOf, you'll get a nullPointerException b/c strReferer is null
0
 

Author Comment

by:894359
ID: 10672668
Thanks.. didn't know that! :) Do now!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
rebasing and merging used in SCM 1 78
J2SE 5.0 metadata 1 61
caughtSpeeding java challenge 16 139
Books that can get me started on JAVA 2 105
IT certifications are a concrete representation of continual learning on the part of the candidate.  Continual learning is necessary for the long term success of an IT professional, but are IT certifications the right path for you?
There's a lot of hype surrounding blockchain technology. Here's how it works and some of the novel ways it' s now being used - including for data protection.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question