Solved

StringBuffer

Posted on 2012-03-27
7
340 Views
Last Modified: 2012-03-29
why would the following sequence cause a null pointer exception on the append?


StringBuffer buff = null;
String line = null;

line = "<test>abc</test>";
buff.append(line);
0
Comment
Question by:dpalyca755
7 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 334 total points
ID: 37774870
StringBuffer buff = null;

buff.append()

if you invoke any method  anything on null that causes nullpointre
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 334 total points
ID: 37774871
StringBuffer buff = new StringBuffer("");


String line = "<test>abc</test>";
buff.append(line);
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 334 total points
ID: 37774872
You cannot invoke any method on null - that is exactly the meanning of null pointer execption - invocation of merthod on null
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:dpalyca755
ID: 37774907
If I plan to work with the StringBuffer and don't know the size do I need to set the capacity?
(I ask because I see the capacity defaulted to 16 characters).
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 334 total points
ID: 37774910
No you don't need to set caparity - you can start form empty string like I showed (not a null though but a string without any charcters!) and then append as much as you want
0
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 83 total points
ID: 37774917
Or use the other constructor, new StringBuffer() which starts as an empty string.

However, are you using Java version 5 or above? And are you doing anything with Threads? If you are version >= 5 and NO threads, then use StringBuilder instead (as recommended in the Javadoc) as it will perform faster.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 83 total points
ID: 37775544
then use StringBuilder instead (as recommended in the Javadoc)

That's correct. You can set the length of the StringBuilder when optimisation is required. Look into the inner workings of StringBuilder to see how to do that best
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle SQL syntax check  without executing 6 75
sql import cannot be resolved jsp 3 48
Notify sent to other threads in Java 9 33
Bot application - advice 3 38
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question