Solved

StringBuffer

Posted on 2012-03-27
7
329 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
for i loop in grovy 1 45
Java Jpanels and Jframe 8 28
eclipse java  build path 6 44
map related example 6 36
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

920 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

14 Experts available now in Live!

Get 1:1 Help Now