Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NULL

 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here why it is throwing null point exception.
as we know that strings are immutable so there is no change in string a .
so why it is not printing null.

but if i remove /////1 and //////2 then it prints null.

How ?/

public class NULL
{

public static void main (String[] args)
{
String a = null;
a.concat("abc");/////1
a.concat("def");/////2
System.out.println(a);
}
}
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In lines 1 and 2, you are trying to call a method on an object reference which is null.

If you take out those lines, you get the String representation of null.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic