When I run my program the first time, the cookie must be a null.
So, for the following statement, why I will get null pointer exception at
runtime? How can I fix it?
if(cookie.getValue()!=null){
...
}else{
...
}
Thanks
> When I run my program the first time, the cookie must be a null.
> So, for the following statement, why I will get null pointer exception at
[quoted text clipped - 5 lines]
> ....
> }
Check if cookie is null first, before trying to access members of it
(like getValue).
For example
if (cookie != null && cookie.getValue() != null)
Note that this counts on the order of evaluation of logical statements,
and if you're not sure, use two if statements.

Signature
Sabine Dinis Blochberger
Op3racional
www.op3racional.eu
Lew - 14 May 2008 13:09 GMT
>> When I run my program the first time, the cookie must be a null.
>> So, for the following statement, why I will get null pointer exception at
[quoted text clipped - 14 lines]
> Note that this counts on the order of evaluation of logical statements,
> and if you're not sure, use two if statements.
If you're not sure of what?

Signature
Lew
Sabine Dinis Blochberger - 14 May 2008 13:19 GMT
>>> When I run my program the first time, the cookie must be a null.
>>> So, for the following statement, why I will get null pointer
[quoted text clipped - 17 lines]
>
> If you're not sure of what?
The order of evaluation. Suppose someone wants a "fast" fix, just be
verbose and don't guess (if you don't want to look it up). *shrugs*

Signature
Sabine Dinis Blochberger
http://www.blochberger.de
Lew - 15 May 2008 04:41 GMT
>>> Note that this counts on the order of evaluation of logical statements,
>>> and if you're not sure, use two if statements.
Lew wrote:
>> If you're not sure of what?
> The order of evaluation. Suppose someone wants a "fast" fix, just be
> verbose and don't guess (if you don't want to look it up). *shrugs*
Anyone more than six months into Java should be aware of the order of
evaluation. Certainly they should be aware that || and && are "early-out"
operators and | and & are not. How could someone claim to be any kind of
experienced Java programmer and not even know the operators?

Signature
Lew
Roedy Green - 14 May 2008 15:55 GMT
On Wed, 14 May 2008 10:40:32 +0100, Sabine Dinis Blochberger
<no.spam@here.invalid> wrote, quoted or indirectly quoted someone who
said :
>Note that this counts on the order of evaluation of logical statements,
>and if you're not sure, use two if statements.
That order is 100% guaranteed. You can count on it. See
http://mindprod.com/jgloss/mccarthyandoperator.html
and
http://mindprod.com/jgloss/mccarthyoroperator.html

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
>if(cookie.getValue()!=null){
cookie might be null too.
So you have to say
if ( cookie== null || cookie.getValue() == null )

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com