• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

help

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class hey{

String s;
hey(){
if(s.length()==0){
System.out.println("Hello");}
else{
System.out.println("Fail");}

}
public static void main(String args[]){
new hey();
}}
when I compile it, I will get NullPointException even if I will check s.length()<0; or s.length()>0;
why is that?
Jaffery
 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
That's because your string variable was not explicitly initialized by you, thus it will be implicitly initialized to null.
Clement
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In other words change your if condition to this -

Due to short circuit nature of && operator, the condition s.length()==0 will be evaluated only when s is not null.
HTH,
- Manish
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jaffery Rab:
when I compile it, I will get NullPointException even if I will check s.length()<0; or s.length()>0;
why is that?
Jaffery

In fact, the s.length() will cause a NullPointerException because you are running a method on a null!
 
Jaffery Rab
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx much guys
Jaffery
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jaffery-
Just a suggestion -- but try giving your posts a descriptive subject instead of "help" give it something like "NullPointerException with String.length()" -- Just an idea
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jessica Sant:
Jaffery-
Just a suggestion -- but try giving your posts a descriptive subject instead of "help" give it something like "NullPointerException with String.length()" -- Just an idea


To go along with what Jessica said - giving your posts a good topic makes them easier to search for. If someone wanted to find something on a NullPointerException, they might never find your thread.
Corey
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic