| Author |
String concat() question required
|
PETER CARTER
Ranch Hand
Joined: Aug 28, 2004
Posts: 70
|
|
Given: 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(X.java:12) 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 null.concat(); 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. Regards Sandeep Jindal
|
 |
PETER CARTER
Ranch Hand
Joined: Aug 28, 2004
Posts: 70
|
|
David, Thanks !!
|
 |
 |
|
|
subject: String concat() question required
|
|
|