aspose file tools*
The moose likes Beginning Java and the fly likes Appending to StringBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Appending to StringBuffer" Watch "Appending to StringBuffer" New topic
Author

Appending to StringBuffer

Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 319
I have been looking at what has to be a simple problem for too long. I thought I just had a basic problem with the for loop, but with Debug, I find the append is not happening. Here's the code:
String pagePath = "CustomPath";
StringBuffer convertedPagePath = null ;
int i = 0;
for ( i = 0; i < pagePath.length(); i++ ) {
if (pagePath.charAt(i) == '/'){
convertedPagePath.append('_');
} // end if
else {
convertedPagePath.append(pagePath.charAt(i));
} // end else
} // end for
I would appreciate any heads up on this. I know this is elementary, but I'm just seeing the same thing over and over.
Betty
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
convertedPagePath is initially null and is never assigned any other value. This code should throw a NullPointerException.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 319
You are right; that is the message I am getting. I have looked in Deitel and Deitel and other places and finally submitted this issue because I didn't see why I was getting this message. What does
do?
Thanks
[ October 26, 2002: Message edited by: Betty Schwartz ]
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
It will append the i'th character of pagePath to convertedPagePath.
But it can't do this if convertedPagePath is null! You need to put a real StringBuffer object into that variable:
StringBuffer convertedPagePath = new StringBuffer();
[ October 26, 2002: Message edited by: Ron Newman ]
Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 319
Ouch! Thanks! I KNEW I was not going to be proud of myself after you told me.
Thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Appending to StringBuffer