Re: checking for null Noob question
Re: checking for null Noob question
- Subject: Re: checking for null Noob question
- From: Mike Schrag <email@hidden>
- Date: Sat, 31 Jul 2010 10:02:08 -0400
+1 for "N".equals(whatever) to prevent NPEs
Sent from my iPhone
On Jul 31, 2010, at 9:44 AM, Theodore Petrosky <email@hidden> wrote:
> Thanks, this was exactly the information I was looking for.
>
> Ted
>
>
> --- On Sat, 7/31/10, Paul Hoadley <email@hidden> wrote:
>
>> From: Paul Hoadley <email@hidden>
>> Subject: Re: checking for null Noob question
>> To: "Theodore Petrosky" <email@hidden>
>> Cc: email@hidden
>> Date: Saturday, July 31, 2010, 8:46 AM
>> Hi Ted,
>>
>> The '||' operator is a 'short circuit' operator, and it
>> evaluates the left-hand side first. Assume
>> this.dueDateType() returns null, then in this case:
>>
>> On 31/07/2010, at 9:35 PM, Theodore Petrosky wrote:
>>
>>> if (this.dueDateType() == null ||
>> this.dueDateType().equals("N")) {
>>
>> the expression on the left-hand side is true, and the
>> right-hand side is never evaluated (because its value won't
>> affect the value of the whole expression). That is, it
>> "short circuits" and you avoid a NullPointerException.
>> In this case:
>>
>>> if (this.dueDateType().equals("N") ||
>> this.dueDateType() == null) {
>>
>> this.dueDateType().equals("N") is evaluated, and you get a
>> NullPointerException.
>>
>>> I don't really care that I must check in a specific
>> order... It just took me time to understand that the order
>> was important.
>>
>> The order is only important to the extent that the LHS is
>> always evaluated, and the RHS is only evaluated if the LHS
>> is false. So in this case, where you're doing a null
>> check specifically to avoid an exception, yes it needs to be
>> on the LHS.
>>
>> http://en.wikipedia.org/wiki/Short-circuit_evaluation
>>
>>
>> --
>> Paul.
>>
>> http://logicsquad.net/
>>
>>
>>
>
>
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Webobjects-dev mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden