• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

doubts in nullpointerexception

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everybody,
My doubt is
1.String s=null;
System.out.println(s.length());
should throw nullpointer exception,but it shows 0.why?
2.for(int i=0; i<N; i++)>
String tmp = "test"; //why this shows compilation error as variable redeclaration
I'll be thankful,if anybody could answer these ques
Thanks,
Mamta
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mamta,
for your first question, I am getting NullPointerException. I was using jdk1.3
Vidya
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) this code definately going to throw nullpointerexception
you prediction is absolutely right.
2) when ever you are going to declare any local or automatic variable create a separate scope there if it could be happened a number of times.
like
for(int i=0;i<10;i++)
String s = "govinda";

the above will definately going to give compilation error stating invalid declaration. do inspite of above as this,
for(int i=0;i<10;i++)
{String s = "govinda";}
putting curley braces will not cause thi error because by this every time program execution flow leave that curley braces String s goes out of scope and hence cease and in the next time
it again get created and after completing that block again ceases. this cycle will goes on up to iteration continues by your for loop.

Originally posted by Mamta Jha:
hi everybody,
My doubt is
1.String s=null;
System.out.println(s.length());
should throw nullpointer exception,but it shows 0.why?
2.for(int i=0; i<N; i++)>
String tmp = "test"; //why this shows compilation error as variable redeclaration
I'll be thankful,if anybody could answer these ques
Thanks,
Mamta


 
Mamta Jha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Vidya & Govinda.
Mamta
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Govinda,
What you say about braces, if also applicable without it. The explanation if valid as it is the only statement inside the loop and should function the same way as with braces.
I am not 100% convinced from conceptual point of view, but the results confirm what you say.
What others have to say?
Regards
 
This tiny ad is guaranteed to be gluten free.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic