8

What's the difference between the DeadObjectException and a NullPointerException? I think I may understand but I just wanted to make sure.


My Understanding

A DeadObjectException happens when you are trying to make a reference to something (and the memory still exists) but there aren't any pointers holding its address, so there's no way to reach that memory. It's different from a NullPointerException in the fact that the memory still is valid, there's just no way to reach it.


Do I have the right idea? Based on other questions on the site:

I think my assertion is correct, I just wanted to make sure.

Community
  • 1
  • 1
Jeeter
  • 5,887
  • 6
  • 44
  • 67
  • a remote object is an object that is tied to an object in another process. When you call methods on it, it acts as a proxy for that other object. when the remote object gets disconnected, the object is no longer reachable. The instance you hold still exists (so, not an NPE), but it refers to something that no longer is. In principle is resemble the NPE, but is much more high level (and entangled in android OS). NPE is what happens when you do `MyObject foo = null; foo.something();` foo is not an objec it is a null reference. – njzk2 Aug 21 '15 at 17:04
  • So (to put it simply) I hold on to a middleman object that holds onto the actual object. When the middleman loses his reference, that's when I get the DOE, because he doesn't know what to execute the commands on? – Jeeter Aug 21 '15 at 17:08
  • @njzk2 is my interpretation of your comment correct? – Jeeter Aug 21 '15 at 17:31
  • @njzk2 yay! do you want to put that as an answer so I can accept it? – Jeeter Aug 21 '15 at 17:41

3 Answers3

1

NullPointerException is when a reference doesn't point to any object within the same Java virtual machine.

DeadObjectException is when a you call a remote object that no longer exists. This happens on 2 different JVM.

pmartin8
  • 1,545
  • 1
  • 20
  • 36
0

NullPointerException - This is thrown when a program tries to access a field or method of an object or an element of an array when there is no instance of an object or array to use. android.os.DeadObjectException - From developer.android.com: As it extends RemoteException The object you are calling has died, because its hosting process no longer exists.

For example:Copied :) its nice example to understand it well.

You have MyActivity and MyService classes. You use Handler/Messenger to communicate between them. You create Handler and Messenger in MyActivity, and then send created instance of Messenger to MyService via Intent. Then you do some stuff, time passes, and your MyActivity gets destroyed, together with it's Handler and Messenger. Now, if you don't handle that well, MyService won't know that Messenger that he has is not valid anymore, so, he tries to send something through it, and get DeadObjectexception:

/* Send a Message to this Messenger's Handler.

Parameters: message The Message to send. Usually retrieved through Message.obtain(). Throws: RemoteException Throws DeadObjectException if the target Handler no longer exists.*/

public void send(Message message) throws RemoteException {...}

Awadesh
  • 3,530
  • 2
  • 20
  • 32
-2

A remote object (e.g. an IBinder) is an object that is tied to an object that can be in another process.

When you call methods on it, it acts as a proxy for that other object. (Instead of you calling directly methods on it, you ask that proxy to transmit your intention and retrieve the result for you.).

When the remote object gets disconnected, the proxy can no longer reach the object, but the proxy object is still present locally.

The instance you hold still exists (so, not an NPE. As long as you have a reference on the instance, it can't disappear. It is impossible to hold a reference and see the instance disappear (some objects will allow similar behavior, such as WeakReference), so you could never get and NPE).

However, it refers to something that no longer is. In principle is resemble the NPE, but is much more high level (and entangled in android OS and the way it handles inter-processes communication).

NPE is what happens when you do

MyObject foo = null;
foo.something();

foo is not an object it is a null reference.

njzk2
  • 38,969
  • 7
  • 69
  • 107