File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String concat() question required Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String concat() question required" Watch "String concat() question required" New topic

String concat() question required

Ranch Hand

Joined: Aug 28, 2004
Posts: 70
11. String a = null;
12. a.concat(�abc�);
13. a.concat(�def�);
14. System.out.println(a);
Why it is :
Exception in thread "main" java.lang.NullPointerException at X.main(
I think it should be "abcdef".

Thanks !!
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
Look into the documentation how concat is defined. It returns the String. Remember that String is IMMUTABLE, so there is no way how to modified it.
Your exmaple is
so it will throws the exception.

SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
Sandeep Jindal
Ranch Hand

Joined: Aug 25, 2003
Posts: 180
Hi Peter,

I think you are new to java/OOP technology.
The answer is in your question only.
You are writing String a = null;
then calling a method of String object on an object that is null.
This is what a null pointer exception is.

In other words, you are trying to c0ncatenate something to an "a" string object, what the jvm will think in which object to concatenate??

I think u just give a thought and it will be clear.

Sandeep Jindal
Ranch Hand

Joined: Aug 28, 2004
Posts: 70
Thanks !!
I agree. Here's the link:
subject: String concat() question required
It's not a secret anymore!