null pointer at run time  
Author Message
a





PostPosted: 2008-5-14 16:59:00 Top

java-programmer, null pointer at run time 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


 
Sabine Dinis Blochberger





PostPosted: 2008-5-14 17:41:00 Top

java-programmer >> null pointer at run time a wrote:

> 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{
> ....
> }
>

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.
--
Sabine Dinis Blochberger

Op3racional
www.op3racional.eu
 
Lew





PostPosted: 2008-5-14 20:10:00 Top

java-programmer >> null pointer at run time Sabine Dinis Blochberger wrote:
> a wrote:
>
>> 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{
>> ....
>> }
>>
>
> 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.

If you're not sure of what?

--
Lew
 
 
Sabine Dinis Blochberger





PostPosted: 2008-5-14 20:20:00 Top

java-programmer >> null pointer at run time Lew wrote:
> Sabine Dinis Blochberger wrote:
>> a wrote:
>>
>>> 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{
>>> ....
>>> }
>>>
>>
>> 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.
>
> 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*

--
Sabine Dinis Blochberger

http://www.blochberger.de
 
 
Roedy Green





PostPosted: 2008-5-14 21:43:00 Top

java-programmer >> null pointer at run time On Wed, 14 May 2008 08:59:29 GMT, "a" <email***@***.com> wrote, quoted or
indirectly quoted someone who said :

>if(cookie.getValue()!=null){

cookie might be null too.

So you have to say

if ( cookie== null || cookie.getValue() == null )
--

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





PostPosted: 2008-5-14 22:56:00 Top

java-programmer >> null pointer at run time On Wed, 14 May 2008 10:40:32 +0100, Sabine Dinis Blochberger
<email***@***.com> 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

--

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





PostPosted: 2008-5-15 11:42:00 Top

java-programmer >> null pointer at run time Sabine Dinis Blochberger wrote:
>>> 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?

Sabine Dinis Blochberger wrote:
> 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?

--
Lew