'Geocoder throws a java.lang.reflect.InvocationTargetException Google Map, Android

I am using map to select a location when long clicking to the google map. But sometimes it throws a java.lang.reflect.InvocationTargetException. I don't know why it works sometimes and why not in other cases. Here is the

error

And here is my code:


        private fun addToHistoryItems(){
        val address = MapHelper.getLocationInfo(requireContext(), selectedLocation)
        val title = address.subThoroughfare+" - "+address.thoroughfare
        val subtitle = "${address.locality}, ${address.countryName}"

        addressSelectionViewModel.insertHistoryItem(AddressHistoryItem(TimeHelper.getCurrentTimeInMillis(),
        selectedLocation.latitude,
        selectedLocation.longitude,
        title,
        subtitle))
    }

    fun getLocationInfo(context : Context, location: LatLng) : Address {
        return Geocoder(context, Locale.getDefault()).getFromLocation(location.latitude, 
            location.longitude, 1)[0]
    }

I think there can be internet problem to produce this error, but I am not sure if it is or not, and if it is how to handle.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source