Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / May 2008

Tip: Looking for answers? Try searching our database.

null pointer at run time

Thread view: 
a - 14 May 2008 09:59 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
runtime? How can I fix it?

if(cookie.getValue()!=null){
...
}else{
...
}

Thanks
Sabine Dinis Blochberger - 14 May 2008 10:40 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 - 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

Roedy Green - 14 May 2008 14:42 GMT
>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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.