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

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 ]
 
I do some of my very best work in water. Like this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic