Solved

StringBuffer

Posted on 2012-03-27
7
326 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
Comment Utility
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
Comment Utility
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
Comment Utility
You cannot invoke any method on null - that is exactly the meanning of null pointer execption - invocation of merthod on null
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:dpalyca755
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now