Exception when trying to get targetGeometry from a viewpoint

901
1
Jump to solution
05-18-2021 08:23 AM
MichaelKrutoyarskiy
New Contributor

I am getting a crash trying to get the targetGeometry inside the body of a addViewpointChangedListener.

The crash happens inconsistently but often, looks like a race condition when the map has just been created and the first viewpoint update is somehow not ready to provide the current viewpoint. 

Reproduced using arcgis-android version 100.10.0 and 100.11.0 but not 100.6.0.

To reproduce add the viewpoint changed listener to the default 'present map' sample project and try to restart the app a few times:

ArcGISRuntimeEnvironment.setApiKey("YOUR_API_KEY")

val map = ArcGISMap(BasemapStyle.ARCGIS_NAVIGATION)

mapView.map = map

mapView.setViewpoint(Viewpoint(34.0270, -118.8050, 72000.0))

mapView.addViewpointChangedListener { event ->
            
   event.source.getCurrentViewpoint(Viewpoint.Type.CENTER_AND_SCALE)?.let { 
      viewpoint ->
      (viewpoint.targetGeometry as? Point)?.let {
         Log.d(TAG, "viewpoint update. Location: ${(GeometryEngine.project(it, SpatialReferences.getWgs84()) as Point)}")
      }
   }
}

 

Stacktrace of the exception:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: me.oriient.arcgisplayground, PID: 14075
    com.esri.arcgisruntime.ArcGISRuntimeException: Nullability violation: ArcGIS Runtime: 1
    Null pointer.
    object cannot be null.
        at com.esri.arcgisruntime.internal.jni.CoreViewpoint.nativeGetTargetGeometry(Native Method)
        at com.esri.arcgisruntime.internal.jni.CoreViewpoint.f(SourceFile:1)
        at com.esri.arcgisruntime.mapping.Viewpoint.getTargetGeometry(SourceFile:2)
        at me.oriient.arcgisplayground.MainActivity$setupMap$1.viewpointChanged(MainActivity.kt:39)
        at com.esri.arcgisruntime.a.i.f.j0$a.run(SourceFile:1)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:6986)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)

 

 

 

 

 

 

 

0 Kudos
1 Solution

Accepted Solutions
MichaelKrutoyarskiy
New Contributor

I've found a way to validate if the viewpoint is ready to be processed. 

if (viewpoint.type == com.esri.arcgisruntime.mapping.Viewpoint.Type.UNKNOWN) {
   return
}

Using this simple check we can ignore the viewpoints which will cause a crash another way.

View solution in original post

0 Kudos
1 Reply
MichaelKrutoyarskiy
New Contributor

I've found a way to validate if the viewpoint is ready to be processed. 

if (viewpoint.type == com.esri.arcgisruntime.mapping.Viewpoint.Type.UNKNOWN) {
   return
}

Using this simple check we can ignore the viewpoints which will cause a crash another way.

0 Kudos