Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

StringBuffer

Posted on 2012-03-27
7
Medium Priority
?
376 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 1336 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 1336 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 1336 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 1336 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 36

Assisted Solution

by:mccarl
mccarl earned 332 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 332 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

730 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