• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

Why is page erroring?

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
894359
Asked:
894359
  • 2
1 Solution
 
bobbit31Commented:
short circuit checks in the wrong order:

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

0
 
bobbit31Commented:
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
 
894359Author Commented:
Thanks.. didn't know that! :) Do now!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now