• 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

NullPointerException

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when I am working on the code given below NullPointerException is throwing ..
code1#
class Ntry

{


public static void main(String args[])

{
String A=null;
String B=null;
String result= A.concat(B);
System.out.println(result);

}
}
But when I am trying with this output nullnull is coming

class Ntry

{


public static void main(String args[])

{
String A=null;
String B=null;
String result= A+B;
System.out.println(result);

}
}
Kindly explain
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

when I am working on the code given below NullPointerException is throwing ..


That is because you are trying to invoke a method of an object which is null

String result= A.concat(B);



But when I am trying with this output nullnull is coming


You have the following code:

String A=null;
String B=null;
String result= A+B;
System.out.println(result);



The println method checks if the String is null. If it is, it allocates the value "null" (String value) to the null String. That is why you get nullnull.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maneesh has explained your problem very well, just if you're courious why is it happenning - check out the Java Language Specification v3 $15.18.1.1
[ August 08, 2008: Message edited by: Dariusz Kordonski ]
 
It's just like a fortune cookie, but instead of a cookie, it's pie. And we'll call it ... tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic