StringBuilder's NullPointerException

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
saregg
Posts: 20
Joined: Fri May 01, 2020 11:27 am
Contact:

StringBuilder's NullPointerException

Post by saregg »

Oracle Docs say:
Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown.
https://docs.oracle.com/en/java/javase/ ... ilder.html
But the book says:
If you pass a null , the string "null" is appended to or inserted in the
existing StringBuilder. No NullPointerException is thrown.
why are these saying antonymous facts, Does they refer to the same context

admin
Site Admin
Posts: 10062
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: StringBuilder's NullPointerException

Post by admin »

They are not contradictory. The statement in Javadoc that you have quoted is a general statement about all methods. The book is giving you the details of the methods after going through what is written for those methods in the javadoc that are important for the exam.
You can try them out by writing some test code and see what happens.
If you like our products and services, please help us by posting your review here.

saregg
Posts: 20
Joined: Fri May 01, 2020 11:27 am
Contact:

Re: StringBuilder's NullPointerException

Post by saregg »

Can you be more specific on which methods ?

admin
Site Admin
Posts: 10062
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: StringBuilder's NullPointerException

Post by admin »

Yes, already mentioned in the book in the paragraph from which you have quoted. All the append and insert methods.
If you like our products and services, please help us by posting your review here.

saregg
Posts: 20
Joined: Fri May 01, 2020 11:27 am
Contact:

Re: StringBuilder's NullPointerException

Post by saregg »

'append' method is overloaded in StringBuilder class: append(String),
append(StringBuffer) and append(char[]) etc. In this case compiler gets
confused as to which method `append(null)` can be tagged because
String, StringBuffer and char[] are not related to each other in multilevel
inheritance. Hence `sb.append(null)` causes compilation error.

--this should be correct ?

saregg
Posts: 20
Joined: Fri May 01, 2020 11:27 am
Contact:

Re: StringBuilder's NullPointerException

Post by saregg »

Code: Select all

public class Test 
	{ public static void main(String[] args) 
 		{ 	StringBuilder sb = new StringBuilder(); 
 			System. out .println(sb.append( null ).length()); 
 		}
 	 }

admin
Site Admin
Posts: 10062
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: StringBuilder's NullPointerException

Post by admin »

Not sure what you are asking. Where is this statement from? What is the result of compiling the code that you have given?
If you like our products and services, please help us by posting your review here.

saregg
Posts: 20
Joined: Fri May 01, 2020 11:27 am
Contact:

Re: StringBuilder's NullPointerException

Post by saregg »

The result of the compilation of code says, ambiguous function call and the reason for that is the explanation given before, but in previous posts you said null can be passed inside append function ? can you explain why ?

admin
Site Admin
Posts: 10062
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: StringBuilder's NullPointerException

Post by admin »

String obj = null;
sb.append(obj);
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 112 guests