Wayne
Wayne

Reputation: 3519

which reference variable is null - Attempt to read from field 'android.os.Handler android.support.v4.a.m.a' on a null object reference

I have a stack trace and it is unclear which reference variable is null.

Attempt to read from field 'android.os.Handler android.support.v4.a.m.a' on a null object reference

if (Looper.myLooper() != mActivity.mHandler.getLooper()) {

(my opinion is that mActivity is null)

  1. mActivity is null ?
  2. mHandler is null ?

A more complete stack Trace - but not part of the question..................

D/AndroidRuntime( 8354): Shutting down VM
E/AndroidRuntime( 8354): FATAL EXCEPTION: main
E/AndroidRuntime( 8354): Process: za.co.nedsecure.nedbankROA.ete, PID: 8354
E/AndroidRuntime( 8354): java.lang.NullPointerException: Attempt to read from field 'android.os.Handler android.support.v4.a.m.a' on a null object reference
E/AndroidRuntime( 8354):at android.support.v4.a.s.f(FragmentManager.java:1476)
E/AndroidRuntime( 8354):at android.support.v4.a.s.b(FragmentManager.java:490)
E/AndroidRuntime( 8354):at xyz.......MainActivity.a(MainActivity.java:299)

MainActivity code is:

if (mainActivityWeakReference.get() != null) {
                    mainActivityWeakReference.get().getSupportFragmentManager().executePendingTransactions();
                }

And my fix:(I hope this is it)

if (mainActivityWeakReference.get() != null && !mainActivityWeakReference.get().isFinishing()) {
                    mainActivityWeakReference.get().getSupportFragmentManager().executePendingTransactions();
                }

Upvotes: 2

Views: 662

Answers (2)

Chris Wilson
Chris Wilson

Reputation: 248

A good way to find this is to put a breakpoint somewhere in the code (right on this line makes sense), and then to evaluate the expression.

You can find Evaluate Expression in the Run menu, or you can use the shortcut which is different depending on whether you are using a pc or mac.

Then put in expressions and hit Evaluate. When you find the one returning null you have found your problem.

So try running all of the possible nulls and you'll eventually find it:

mActivity
mActivity.mHandler

Upvotes: 3

OldCurmudgeon
OldCurmudgeon

Reputation: 65813

You must read more of the stack trace for more clues but really the easiest way to find these is to put the expression across multiple lines.

if (Looper
  .myLooper() 
  != 
  mActivity
  .mHandler
  .getLooper()) {

Please remember to undo this before comitting the source.

Upvotes: 3

Related Questions