Thomas Calc
Thomas Calc

Reputation: 3014

Android doc: constructor returning null for SoundPool?

I know the general matter has been discussed in this stackoverflow topic already (constructors never return null), but considering its significance in this concrete matter, I would like to know if the official Android documentation of the SoundPool constructor is wrong then:

Constructor. Returns a SoundPool object, or null if creation failed

I'm asking this because we're talking about no less than the official documentation of Android that has been up for years (in case of SoundPool).

Checking the Android source code, the SoundPool throws a RuntimeException in case of an error in constructor. (The pre-2.3 Android did not even throw an exception.) Perhaps the documentation tries to express that if the exception is caught, then the variable where I intended to store the object reference remains null? In this case, the doc is still very poorly worded. Am I missing something?

EDIT: considering this might not be a very content-rich question (even though it may be useful -- see my comments), a simple Yes or No would be sufficient, and then the thread can get closed. I want to make sure I haven't overlooked anything.

Upvotes: 3

Views: 180

Answers (1)

Joseph Earl
Joseph Earl

Reputation: 23442

  1. Constructors cannot return null. They can throw exceptions, but they cannot return anything but the object which they create if successful.
  2. As you have found, SoundPool may throw an exception in it's constructor. You can ignore it. They specifically chose to throw a RuntimeException (on 2.3+) as opposed to a checked exception which indicates the API designers consider this a good enough reason to crash an app and that does not need to be routinely handled.
  3. If you really must handle the exception then I would simply notify the user and quit immediately.

So no, it can't return null. But if you handle the exception then yes, your reference to the object will be null because the constructor did not complete.

Upvotes: 1

Related Questions