From 73b6d0519ff25459f82d0020ccd3322f26988dc0 Mon Sep 17 00:00:00 2001 From: Eric House Date: Fri, 28 Aug 2020 08:32:23 -0700 Subject: [PATCH] fix NPE There was a way to get to the sender before its handler had been initialized. That can only happen on UI thread, so just drop the send rather than figure out a better way (for now). --- .../eehouse/android/xw4/NetStateCache.java | 54 ++++++++++--------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/NetStateCache.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/NetStateCache.java index e14772d6e..81fc7a0f1 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/NetStateCache.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/NetStateCache.java @@ -29,7 +29,6 @@ import android.net.NetworkInfo; import android.os.Build; import android.os.Handler; - import org.eehouse.android.xw4.jni.CommsAddrRec.CommsConnType; import java.util.HashSet; @@ -235,37 +234,40 @@ public class NetStateCache { // will only fire if we go that long without coming // through here again. - if ( null != mNotifyLater ) { - mHandler.removeCallbacks( mNotifyLater ); - mNotifyLater = null; - } - if ( mLastStateSent != s_netAvail ) { - mNotifyLater = new Runnable() { - @Override - public void run() { - if ( mLastStateSent != s_netAvail ) { - mLastStateSent = s_netAvail; + if ( null == mHandler ) { + Log.e( TAG, "notifyStateChanged(): handler null so dropping" ); + } else { + if ( null != mNotifyLater ) { + mHandler.removeCallbacks( mNotifyLater ); + mNotifyLater = null; + } + if ( mLastStateSent != s_netAvail ) { + mNotifyLater = new Runnable() { + @Override + public void run() { + if ( mLastStateSent != s_netAvail ) { + mLastStateSent = s_netAvail; - Log.i( TAG, "notifyStateChanged(%b)", s_netAvail ); + Log.i( TAG, "notifyStateChanged(%b)", s_netAvail ); - synchronized( s_ifs ) { - Iterator iter = s_ifs.iterator(); - while ( iter.hasNext() ) { - iter.next().onNetAvail( s_netAvail ); + synchronized( s_ifs ) { + Iterator iter = s_ifs.iterator(); + while ( iter.hasNext() ) { + iter.next().onNetAvail( s_netAvail ); + } + } + + if ( s_netAvail ) { + CommsConnType typ = CommsConnType + .COMMS_CONN_RELAY; + GameUtils.resendAllIf( context, typ ); } } - - if ( s_netAvail ) { - CommsConnType typ = CommsConnType - .COMMS_CONN_RELAY; - GameUtils.resendAllIf( context, typ ); - } } - } - }; - mHandler.postDelayed( mNotifyLater, WAIT_STABLE_MILLIS ); + }; + mHandler.postDelayed( mNotifyLater, WAIT_STABLE_MILLIS ); + } } } } // class PvtBroadcastReceiver - }