File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Adding nulls to stringbuffers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Adding nulls to stringbuffers" Watch "Adding nulls to stringbuffers" New topic
Author

Adding nulls to stringbuffers

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

According to the API:

The characters of the String argument are appended, in order, increasing the length of this sequence by the length of the argument. If str is null, then the four characters "null" are appended.


I tried to test this out as shown below:



But it is producing NullpointExceptions



Sebanti Sanyal
Ranch Hand

Joined: Nov 07, 2011
Posts: 58

The reference 'sb' is itself null here and therefore, invoking methods using sb causes NullpointerException. You may verify whatever the API says as follows:
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Sebanti Sanyal wrote:The reference 'sb' is itself null here


Doesnt StringBuffer sb = new StringBuffer(null); make it not null?
Sebanti Sanyal
Ranch Hand

Joined: Nov 07, 2011
Posts: 58

Doesnt StringBuffer sb = new StringBuffer(null); make it not null?

No it doesn't. Instead, 'new StringBuffer()' creates a new StringBuffer object with no content.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

You cannot pass null to the StringBuffer constructor. Try this instead.
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

James Boswell wrote:You cannot pass null to the StringBuffer constructor. Try this instead.


I did try this before but it gives an error because multiple append() methods can allow nulls

James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

Ah good point.

I think Sebanti has this right:

 
Don't get me started about those stupid light bulbs.
 
subject: Adding nulls to stringbuffers