RemoteCallbackList.getBroadcastItem(int) sometimes returns null

by Streets Of Boston » Fri, 13 Mar 2009 09:08:11 GMT

Sponsored Links

I found out that RemoteCallbackList.getBroadcastItem(int) sometimes
returns null.  I have this code in my Service and it raises a null-
pointer exception:

    // instance variable of my Service
    final RemoteCallbackList<IProgressCallback> mCallbacks
            = new RemoteCallbackList<IProgressCallback>();

public boolean sendProgressCallback(String imgUri, int albumId, int
progressBytes, int progressPercent) {
        final QueuedImageUpload qiu = getUploadingImage(imgUri, albumId);
        if (qiu == null)
            return true;


        final int totalSizePercent = mQueues.calculateRemainingSizePercent();
        final int N = mCallbacks.beginBroadcast();
        try {
                for (int i = 0; i < N; i++) {
                        try {
imgUri, albumId, progressPercent, totalSizePercent)) {
                                        return false;
                        } catch (RemoteException e) {
                                // The RemoteCallbackList will take care of 
                                // the dead object for us.
        finally {
        return true;

This line is the one throwing a nullpointer exception:
"if (!mCallbacks.getBroadcastItem(i).progressChanged(qiu.getID(),
imgUri, albumId, progressPercent, totalSizePercent))"

- The mCallbacks is never null. It is declared as final.
- The local 'qui' stack-variable is never null. It is guarded by a
null-pointer check.

That only leads me to conclude that the call getBroadcastItem(i)
returns null. However, the documentation states that this method never
returns null. Is this a bug in Android?

This problem only happens on occasion (I can not reproduce it
consistently), but it happens often enough to be of concern for my

BTW, someone else reported the same problem a while ago:

RemoteCallbackList.getBroadcastItem(int) sometimes returns null

by Streets Of Boston » Fri, 13 Mar 2009 11:08:48 GMT

 mmmmmmmm.... you mean that the value of 'N 'could be larger than the
actual elements accessed by getBroadcastItem(i), i.e. HashMap changes
while looping over the 'N' items...?

For now, i'm check for null and just ignore it. The problem is that it
is very hard to reproduce. It happens on rare occasions, but often
enough to be problematic.

This line has only 3 points in which it could throw a null-pointer
exception (the stack-trace reports the NPE on this line, not inside
- mCallbacks is null: Not possible. It is declared final and refers to
an instance.
- qiu is null: Not possible. It is a stack-variable and it has been
guarded against null value.
- getBroadcastItem(i): The only one left...

I thought that beginBroadcast() would lock the callback-list for
modification and finishBroadcast() would unlock it again. This way, N
should always be the number of items in this HashMap you were
mentioning. Am i correct in this.

I'll let you know if i can figure out which values for 'i' are
problematic :-)

Thanks for your help.

On Mar 12, 9:27pm, Dianne Hackborn <> wrote:

Sponsored Links

RemoteCallbackList.getBroadcastItem(int) sometimes returns null

by Streets Of Boston » Fri, 13 Mar 2009 22:27:37 GMT

 Ah, thanks.

Either way, it means that the number of items in the list accessed by
getBroadcastItem is always equal to N.
The problem remains: How can getBroadcastItem(int) return null?


RemoteCallbackList.getBroadcastItem(int) sometimes returns null

by Streets Of Boston » Sat, 14 Mar 2009 01:56:08 GMT

 Ah, now i get it! :-)

Indeed, sendProgressCallback can be called from multiple threads. And
with your earlier explanation that the beginBroadcast does not do
locking (only a brief internal lock), it may explain the problem. In
other words, RemoteCallbackList is not thread-safe across multiple
calls to it.

I should put the broadcast code inside a 'synchronized(mCallbacks)
{ ... }' block or surround it with a ReentrantLock.
Or create mCallbacks instances for each possible thread and have my
listeners register with each of them.

Thank you very much for your help.
Have a great weekend!


RemoteCallbackList.getBroadcastItem(int) sometimes returns null

by Streets Of Boston » Sat, 14 Mar 2009 03:50:14 GMT

 las, this one is not a one-way interface. It expects a boolean back:
False = cancel upload of image, True = continue uploading image. It
allows the (remote) client to cancel uploads forcefully. Luckily, the
client receiving the call-back can return this answer really fast.

The client is not the same process. It is a different app (Activity)
in a different process.

What is your opinion, in terms of speed/performance: One 'mCallbacks'
instance for the Service, sending this callback to its clients, inside
a synchronized(...) block or having each a 'mCallbacks' for each
thread (max 3)? Which one would be better?
The client is always remote (other process).

On Mar 13, 2:29pm, Dianne Hackborn <> wrote:

Other Threads

1. YAFFS2 filesystem in Android linux?

Hi all,
I am a newbie to Android kernel development.  The robustness of the file
system is very critical for a mobile
device.  YAFFS2 is a file system that was designed specifically for NAND
flash, which is supposed to be more robust than EXT3 file system in terms of
NAND flash.  I wonder if there is YAFF2 support in Android platform? or
there is an alternative other than EXT3?

Any hint is very appreciated.



2. conatct/calender/dialer does not launch


I have android 1.5 running on developer board which does not have
touhchscreen and all keboard keys are passed through UART. All
application works except contact, calender and dialer. When these
applciations are launched "android.process.acore has stopped
unexpectdegly" error comes wth force close.

Below is the log and It s not clear resourcetype get is failing and
ultimatly VM is shutting down.   Any idea?

# D/dalvikvm(  887): GC freed 315 objects / 14056 bytes in 113ms
^[[CW/KeyCharacterMap(  887): No keyboard for id 0
W/KeyCharacterMap(  887): Using default keymap: /system/usr/keychars/
: not found
# I/ActivityManager(  518): Starting activity: Intent
tion.MAIN categories={android.intent.category.LAUNCHER}
flags=0x10200000 comp={c} }
W/InputManagerService(  518): Starting input on non-focused client
nternal.view.iinputmethodclient$stub$pr...@4376d8c8 (uid=10004
W/ResourceType(  887): Failure getting entry for 0x7f030005 (t=2 e=5)
in package
 0: 0xffffffb5
D/AndroidRuntime(  887): Shutting down VM
W/dalvikvm(  887): threadid=3: thread exiting with uncaught exception
E/AndroidRuntime(  887): Uncaught handler: thread main exiting due to
uncaught e
E/AndroidRuntime(  887): java.lang.RuntimeException: Unable to start
activity Co
mponentInfo{}: androi
d.content.res.Resources$NotFoundException: Resource ID #0x7f030005
E/AndroidRuntime(  887):        at
E/AndroidRuntime(  887):        at
E/AndroidRuntime(  887):        at$1800
E/AndroidRuntime(  887):        at$H.handleMessage
E/AndroidRuntime(  887):        at android.os.Handler.dispatchMessage
E/AndroidRuntime(  887):        at android.os.Looper.loop(
E/AndroidRuntime(  887):        at
E/AndroidRuntime(  887):        at java.lang.reflect.Method.invokeNative
E/AndroidRuntime(  887):        at java.lang.reflect.Method.invoke
E/AndroidRuntime(  887):        at
E/AndroidRuntime(  887):        at
E/AndroidRuntime(  887):        at dalvik.system.NativeStart.main(Native
E/AndroidRuntime(  887): Caused by: android.content.res.Resources
ion: Resource ID #0x7f030005
E/AndroidRuntime(  887):        at android.content.res.Resources.getValue
E/AndroidRuntime(  887):        at
E/AndroidRuntime(  887):        at android.content.res.Resources.getLayout
E/AndroidRuntime(  887):        at android.view.LayoutInflater.inflate
E/AndroidRuntime(  887):        at android.view.LayoutInflater.inflate
E/AndroidRuntime(  887):        at
E/AndroidRuntime(  887):        at
E/AndroidRuntime(  887):        at
E/AndroidRuntime(  887):        at
E/AndroidRuntime(  887):        at
E/AndroidRuntime(  887):        ... 11 more
I/Process (  518): Sending signal. PID: 887 SIG: 3
I/dalvikvm(  887): threadid=7: reacting to signal 3
I/dalvikvm(  887): Wrote stack trace to '/data/anr/traces.txt'
D/dalvikvm(  518): GC freed 9343 objects / 436720 bytes in 172ms
E/ActivityThread(  518): Failed to find provider info for
E/Checkin (  518): Error reporting crash:
java.lang.IllegalArgumentException: Un
known URL content://android.server.checkin/crashes
W/ActivityManager(  518): Launch timeout has expired, giving up wake
W/ActivityManager(  518): Activity idle timeout for HistoryRecord
{43767d18 {com.


3. how to create a custom listener for some objects..

4. booting up the emulator with custom kernel build

5. cannot find htonl

6. Help with WiFinder

7. Is it possible to change text color of all child TextViews