1. Are you ready for the Galaxy S20? Here is everything we know so far!

Null object reference in coder: Geocoder = Geocoder(context)

Discussion in 'Android Development' started by nagamothu, Mar 4, 2020.

  1. nagamothu

    nagamothu Newbie
    Thread Starter

    Hii i have a doubt in maps fragment. i have 8 markers to display in maps. i have done it like this.
    This code is working perfectly fine. but i want to implement MVVM in this and i want keep all the location addresses in array list . when i have done that am getting null object reference in getLocationfromaddress() method at this line
    val coder: Geocoder = Geocoder(context)

    how to place all locations in arraylist

    Code (Text):
    1.  
    2.  
    3. class MapsActivity :AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnMarkerClickListener
    4. {
    5.  
    6.     private lateinit var mMap: GoogleMap
    7.  
    8.     private lateinit var button_res: Button
    9.     lateinit var address: LatLng
    10.  
    11.     lateinit var pietrasanta: LatLng
    12.     lateinit var Ikea_alexandra: LatLng
    13.     lateinit var candlenut_resturant: LatLng
    14.     lateinit  var MDIS_Dhoby: LatLng
    15.     lateinit  var ridges: LatLng
    16.     lateinit var hilton: LatLng
    17.     lateinit  var library: LatLng
    18.     lateinit var tanglin: LatLng
    19.  
    20.     companion object {
    21.         private val MY_PERMISSION_FINE_LOCATION = 101
    22.     }
    23.  
    24.     @RequiresApi(Build.VERSION_CODES.N)
    25.     override fun onCreate(savedInstanceState: Bundle?) {
    26.         super.onCreate(savedInstanceState)
    27.         setContentView(R.layout.activity_maps)
    28.  
    29.         val mapFragment = supportFragmentManager
    30.             .findFragmentById(R.id.map) as SupportMapFragment
    31.         mapFragment.getMapAsync(this)
    32.     }
    33.  
    34.  
    35.     /**
    36.      * Manipulates the map once available.
    37.      * This callback is triggered when the map is ready to be used.
    38.      * This is where we can add markers or lines, add listeners or move the camera. In this case,
    39.      * we just add a marker near Sydney, Australia.
    40.      * If Google Play services is not installed on the device, the user will be prompted to install
    41.      * it inside the SupportMapFragment. This method will only be triggered once the user has
    42.      * installed Google Play services and returned to the app.
    43.      */
    44.  
    45.  
    46.     override fun onMapReady(googleMap: GoogleMap) {
    47.         mMap = googleMap
    48.  
    49.         address = getLocationFromAddress(  this, "ADDRESS 1 " ) //this will be main marker and in the middle of the maps activity
    50.         mMap.addMarker(
    51.             MarkerOptions().position(address/*LatLng(17.373838, 78.533271)*/).title("MDIS")
    52.                 .snippet("501 Stirling Rd,Singapore 148951")
    53.                 .icon(BitmapDescriptorFactory.fromResource(R.drawable.icon))
    54.         )
    55.             .showInfoWindow()
    56.         mMap.moveCamera(CameraUpdateFactory.newLatLng(address))
    57.         mMap.animateCamera(CameraUpdateFactory.zoomIn())
    58.         mMap.animateCamera(CameraUpdateFactory.zoomTo(15F), 2000, null)
    59.         mMap.uiSettings.setAllGesturesEnabled(true)
    60.         mMap.uiSettings.isCompassEnabled = true
    61.  
    62.     //the following markers are the nerby markers for address marker
    63.          pietrasanta= getLocationFromAddress(this,"ADDRESS 2")
    64.          Ikea_alexandra= getLocationFromAddress(this,"ADDRESS 3")
    65.          candlenut_resturant = getLocationFromAddress(this, "ADDRESS 4")
    66.          MDIS_Dhoby= getLocationFromAddress(this, " ADDRESS 5  ")
    67.          ridges = getLocationFromAddress(this, "ADDRESS 6")
    68.          hilton =  getLocationFromAddress(this, "ADDRESS 7")
    69.          library= getLocationFromAddress(this, "ADDRESS 8")
    70.          tanglin = getLocationFromAddress(this, "ADDRESS 9")
    71.  
    72.  
    73.        mMap.addMarker(MarkerOptions().position(pietrasanta).title("Pietrasanta the Italian Resturant"))
    74.  
    75.         mMap.addMarker(MarkerOptions().position(Ikea_alexandra).title("Ikea alexandra resturant"))
    76.  
    77.         mMap.addMarker(MarkerOptions().position(candlenut_resturant).title("Candlenut Resturant"))
    78.  
    79.         mMap.addMarker(MarkerOptions().position(MDIS_Dhoby).title("MDIS Dhoby Ghaut"))
    80.  
    81.  
    82.         mMap.addMarker(MarkerOptions().position(ridges).title("Southern Ridges"))
    83.  
    84.  
    85.         mMap.addMarker(MarkerOptions().position(hilton).title("Hilton Singapore"))
    86.  
    87.  
    88.         mMap.addMarker(MarkerOptions().position(library).title("Queenstown Public Library"))
    89.  
    90.  
    91.         mMap.addMarker(MarkerOptions().position(tanglin).title("Tanglin Halt Food Centre"))
    92.  
    93.  
    94.  
    95.  
    96.         if (ActivityCompat.checkSelfPermission(
    97.                 this,
    98.                 Manifest.permission.ACCESS_FINE_LOCATION
    99.             ) == PackageManager.PERMISSION_GRANTED
    100.         ) {
    101.             mMap.isMyLocationEnabled = true
    102.  
    103.         } else {//condition for Marshmello and above
    104.             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    105.                 requestPermissions(
    106.                     arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
    107.                     MY_PERMISSION_FINE_LOCATION
    108.                 )
    109.             }
    110.         }
    111.  
    112.    
    113.         fun getLocationFromAddress(context: Context, strAddress: String): LatLng {
    114.             val coder: Geocoder = Geocoder(context)
    115.             val address1: List<Address>
    116.             lateinit var p1: LatLng
    117.  
    118.  
    119.             try {
    120.                 address1 = coder.getFromLocationName(strAddress, 5)
    121.                 if (address1 == null) {
    122.  
    123.                 }
    124.                 val location: Address = address1.get(0)
    125.                 location.latitude
    126.                 location.latitude
    127.  
    128.                 p1 = LatLng(location.latitude, location.longitude)
    129.  
    130.  
    131.             } catch (e: Exception) {
    132.                 e.printStackTrace()
    133.             }
    134.  
    135.             return p1
    136.         }
    137.  
    138.  
    139.     override fun onMarkerClick(p0: Marker?) = true
    140.  
    141.  
    142.     override fun onRequestPermissionsResult(
    143.         requestCode: Int,
    144.         permissions: Array<String>,
    145.         grantResults: IntArray
    146.     ) {
    147.         super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    148.         when (requestCode) {
    149.             MY_PERMISSION_FINE_LOCATION -> if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {//permission to access location grant
    150.                 if (ActivityCompat.checkSelfPermission(
    151.                         this,
    152.                         Manifest.permission.ACCESS_FINE_LOCATION
    153.                     ) == PackageManager.PERMISSION_GRANTED
    154.                 ) {
    155.                     mMap.isMyLocationEnabled = true
    156.                 }
    157.             }
    158.             //permission to access location denied
    159.             else {
    160.                 Toast.makeText(
    161.                     applicationContext,
    162.                     "This app requires location permissions to be granted",
    163.                     Toast.LENGTH_LONG
    164.                 ).show()
    165.                 finish()
    166.             }
    167.         }
    168.     }
    169.  
    170.  
    171.  
    172. }
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - Null object reference
  1. abc8773
    Replies:
    2
    Views:
    341
  2. DridriLaBastos
    Replies:
    0
    Views:
    470
  3. Frederik Kammel
    Replies:
    1
    Views:
    571
  4. acenspades
    Replies:
    0
    Views:
    725
  5. Ahmad Rodzaidi Ahmad Faiz
    Replies:
    0
    Views:
    2,214
  6. MamtaJatRana
    Replies:
    0
    Views:
    974
  7. Abdii
    Replies:
    0
    Views:
    386
  8. Giouri Kilinkaridis
    Replies:
    0
    Views:
    1,133
  9. JamieKitson
    Replies:
    1
    Views:
    1,310
  10. anwartan
    Replies:
    0
    Views:
    1,114

Share This Page

Loading...