• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Need help to understand reference type declaration and null comparison.

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.






 
Ranch Hand
Posts: 270
15
Android Angular Framework Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You L Foster and Henry Wong for helping me understand.

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 270
15
Android Angular Framework Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76395
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
What? What, what, what? What what tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic