Tuesday, June 22, 2010

Re: [android-developers] Need help with a Java issue

Hey Toni.

Yes, I verified that this is the exact spot where the NPE occurs. More than 10 times ;) It would be possible though that the tag I used wasn't accurate, but even then I wouldn't know which of the variables could be null and fail in that way. The databaseHelper is also initialized in the constructor.

No, I didn't leave out the synchronization, but I think it wouldn't make any difference as this would "only" affect if an item is in the Map, not if the Map is instantiated/null. This should be concluded after the constructor is left and the object is kicked into life, right?
Just because I am so unsure, I tried to call Map.get as well as Map.put with null and interestingly enough it works. If I put some Object with a key of null into the Map I get the same object back when I access the Map again with null as the key. But that is actually not really relevant here – I think – as the NPE happens right on the last line and not inside the Map implementation, which would be the case if the Map would have an issue with a null.

Thanks for helping me to think here ;)

Cheers,
Mariano

On Tue, Jun 22, 2010 at 12:08 PM, Toni Menzel <toni@okidokiteam.com> wrote:
Hi Mariano,

First of all, did you leave out synchronization for simplicity in this
mail or is it not in place at all ?
(assuming you do access EntityManager from multiple threads).

Also, pls doublecheck that EntryManager.java:1529 is really at the
spot you indicated (cannot verify with complete sources).
Specifically i am unsure about databaseHelper. Its just accessed in
you example, so could be null.

At best give the full file so we can check.

Toni

On Tue, Jun 22, 2010 at 11:59 AM, Mariano Kamp <mariano.kamp@gmail.com> wrote:
> Hey guys.
> This is not an Android issue per-se, but I am puzzled about a Java issue and
> want to pick your brains.
> I recently got a couple of bug reports with a NPE that I don't understand
> and can't reproduce myself. Here is an abbreviated representation of the
> offending class. I tried to stay as close to the actual code as possible.
> public class EntryManager {
>   private static EntryManager instance;
>   private Map<DBQuery, Boolean> isMarkAllReadPossibleCache;
>   private EntryManager(Context ctx) {
>     ..
>     isMarkAllReadPossibleCache = new HashMap<DBQuery, Boolean>(); 
>     databaseHelper = new DB(ctx);
>     ..
>   }
>   public static synchronized EntryManager getInstance(final Context context)
> {
>        if (instance == null)
>            instance = new EntryManager(context.getApplicationContext());
>        return instance;
>    }
>    void fireModelUpdated(final String atomId) {
>         ..
>         isMarkAllReadPossibleCache.clear();
>         ..
>    }
>   public boolean isMarkAllReadPossible(final DBQuery dbq) {
>         if (!isMarkAllReadPossibleCache.containsKey(dbq))
>             isMarkAllReadPossibleCache.put(dbq,
> databaseHelper.isMarkAllReadPossible(dbq));
>        return isMarkAllReadPossibleCache.get(dbq); <!-- NPE is raised here
>     }
>    ..
> }
> In the last line from above a NPE is thrown. So the instance variable
> isMarkAllReadPossibleCache must be null, right? But I don't see where this
> could happen. It's initialized in the constructor and never set to null
> afterwards.
> For completeness here is a stack trace:
>
> java.lang.NullPointerException
> at com.newsrob.EntryManager.isMarkAllReadPossible(EntryManager.java:1529)
> at
> com.newsrob.activities.AbstractNewsRobListActivity.shouldMarkAllReadButtonBeEnabled(AbstractNewsRobListActivity.java:327)
> at
> com.newsrob.activities.AbstractNewsRobListActivity.updateButtons(AbstractNewsRobListActivity.java:236)
> at
> com.newsrob.activities.AbstractNewsRobListActivity.access$2(AbstractNewsRobListActivity.java:215)
> <-- removed the private access modifier meanwhile
> at
> com.newsrob.activities.AbstractNewsRobListActivity$13.run(AbstractNewsRobListActivity.java:626)
> at android.os.Handler.handleCallback(Handler.java:587)
> at android.os.Handler.dispatchMessage(Handler.java:92)
> at android.os.Looper.loop(Looper.java:123)
> at android.app.ActivityThread.main(ActivityThread.java:4203)
> at java.lang.reflect.Method.invokeNative(Native Method)
> at java.lang.reflect.Method.invoke(Method.java:521)
> at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
> at dalvik.system.NativeStart.main(Native Method)
> Any ideas?
> Cheers,
> Mariano
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscribe@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en



--
Toni Menzel
Independent Software Developer
Professional Profile: http://okidokiteam.com
toni@okidokiteam.com
http://www.ops4j.org     - New Energy for OSS Communities - Open
Participation Software.

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment