Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help to understand reference type declaration and null comparison.  RSS feed

 
Joy Vergis
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
Please help me understand



With above, I am using the reference of UserRelated to access the method checkUserInput(String value) as below, it returns NULL Pointer Exception



But when I try executing the above code after making the below changes it works as expected.

I believe as this is instance method after making changes as per line no. (2) it works.


I am unable to understand that if instance of UserRelated is not created at line no. (1), then why method checkUserInput(fname) is accessible at line no (b) and never executes

Also if null cannot be compared to anything please help let me know if line no.(a) is correct for checking null.






 
L Foster
Ranch Hand
Posts: 242
14
Android Angular Framework AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings, Joy,

The line below, excerpted from your post, is doing things in the wrong order. Obviously, your if-block is there to check whether anything was entered or not, for that value. However, as you probably know, a double-pipe OR test will be evaluated until a "true" is encountered. If no "true" condition is found, the if test will evaluate to "false".

if(value.equals("") || (value==null))

If 'value' contains a null (second condition is true), but you dereference it for the first test (equals), then you are dereferencing a null pointer.

Just switch the order of these two.

That being said, and this is for your benefit: if you really did not know that the 'if' evaluates things in that order, or that the "equals" test has to have had something set in "value", or it will have a null pointer, you should be reading up on basic Java. If you are lacking funds, go straight to the tutorials at Oracle. Otherwise, I would invest in a book like "Java in a Nutshell", from O'Reilly. That is not the only book out there; others will tell you there are other books they prefer. The O'Reilly was one of my starters, and I liked it. Get the latest edition.

Regards
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joy Vergis wrote:
I am unable to understand that if instance of UserRelated is not created at line no. (1), then why method checkUserInput(fname) is accessible at line no (b) and never executes


Not sure what you mean by checkUserInput(fname) is accessible, as you mentioned eariler ...

Joy Vergis wrote:
With above, I am using the reference of UserRelated to access the method checkUserInput(String value) as below, it returns NULL Pointer Exception


The method isn't accessible. And threw a NullPointerException, when the runtime tried to call the method. How did you conclude that the method is called, when it wasn't?

Henry
 
Joy Vergis
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You L Foster and Henry Wong for helping me understand.

 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
L Foster wrote: if(value.equals("") || (value==null))
If 'value' contains a null (second condition is true), but you dereference it for the first test (equals), then you are dereferencing a null pointer.
Just switch the order of these two.

Or alternatively, swap the first comparison around, ie
  if("".equals(value) ...

@Joy: L.Foster's advice is definitely better, because the '== null' check is likely to be much quicker; but I just thought I'd let you know about that little 'trick'.

Winston
 
L Foster
Ranch Hand
Posts: 242
14
Android Angular Framework AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Winston, I like doing things that way (check if the known-non-null value 'equals' the possibly-null value). However, since this if-block is attempting to eliminate two different possibilities, the null check still must be done anyway. Null is not equal to empty string.

@Joy, do keep Winston's suggestion in your tool kit. It will avoid throwing null pointers, regardless.
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
L Foster wrote: . . . invest in a book like "Java in a Nutshell", from O'Reilly. . . . I liked it. . . .
I have only looked at that book very briefly but I didn't like it. I thought it spent too much time simply copying the API documentation, which you can find online anyway.

But other people will have different opinions.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!