Error on "Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference"

14,850

Solution 1

Your latitude and longitude are NULL, as they are only declared and value never assigned.

Example :

latitude=28.6139391;

longitude=77.2068325;

set location like this, this location is of Delhi

Solution 2

Try and assign value to latitude and longtitude-:

import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    import android.support.v7.app.ActionBarActivity;
    import android.support.v7.app.AppCompatActivity;

    import com.google.android.gms.maps.CameraUpdateFactory;
    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.OnMapReadyCallback;
    import com.google.android.gms.maps.SupportMapFragment;
    import com.google.android.gms.maps.model.BitmapDescriptorFactory;
    import com.google.android.gms.maps.model.CameraPosition;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.android.gms.maps.model.MarkerOptions;

    import static com.example.kod45.refapp.R.id.lat;
    import static com.example.kod45.refapp.R.id.longitude;

    public class Map extends FragmentActivity implements OnMapReadyCallback {
        Double latitude;
        Double longitude;
        private GoogleMap mMap;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_map);
            // Obtain the SupportMapFragment and get notified when the map is ready to be used.
            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);

add value to latitude and longtitude from where are you getting "latitude"= "longtitude"= mapFragment.getMapAsync(this); }

        @Override
        public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;

            // Add a marker in Tallaght and move the camera

            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(new LatLng(latitude, longitude))  //here latitude and longtitude are null as no value is assigned to them    // Sets the center of the map to Mountain View
                    .zoom(17)                   // Sets the zoom
                    .bearing(90)                // Sets the orientation of the camera to east
                    .tilt(30)                   // Sets the tilt of the camera to 30 degrees
                    .build();                   // Creates a CameraPosition from the builder
            mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

            Intent intent = getIntent();
            intent.putExtra("latitude", latitude);  
            intent.putExtra("longitude", longitude);
    //        LatLng sydney = new LatLng(latitude, longitude);

        }
    }

Solution 3

You never assigned values to

Double latitude;
Double longitude;

This is where you used them.

 .target(new LatLng(latitude, longitude))      // Sets the center of the map to Mountain View
Share:
14,850
Rick Sanchez
Author by

Rick Sanchez

Updated on June 07, 2022

Comments

  • Rick Sanchez
    Rick Sanchez almost 2 years

    I have just been having an issue with displaying a map by accessing the Latitude and Longitude from the SqliteDatabase.

    When I run this I get the above error on the line .target(new LatLng(latitude, longitude)). Any help would be appreciated.

     import android.content.Intent;
        import android.os.Bundle;
        import android.support.v4.app.FragmentActivity;
        import android.support.v4.app.FragmentManager;
        import android.support.v7.app.ActionBarActivity;
        import android.support.v7.app.AppCompatActivity;
    
        import com.google.android.gms.maps.CameraUpdateFactory;
        import com.google.android.gms.maps.GoogleMap;
        import com.google.android.gms.maps.OnMapReadyCallback;
        import com.google.android.gms.maps.SupportMapFragment;
        import com.google.android.gms.maps.model.BitmapDescriptorFactory;
        import com.google.android.gms.maps.model.CameraPosition;
        import com.google.android.gms.maps.model.LatLng;
        import com.google.android.gms.maps.model.MarkerOptions;
    
        import static com.example.kod45.refapp.R.id.lat;
        import static com.example.kod45.refapp.R.id.longitude;
    
        public class Map extends FragmentActivity implements OnMapReadyCallback {
            Double latitude;
            Double longitude;
            private GoogleMap mMap;
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_map);
                // Obtain the SupportMapFragment and get notified when the map is ready to be used.
                SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                        .findFragmentById(R.id.map);
                mapFragment.getMapAsync(this);
            }
    
            @Override
            public void onMapReady(GoogleMap googleMap) {
                mMap = googleMap;
    
                // Add a marker in Tallaght and move the camera
    
                CameraPosition cameraPosition = new CameraPosition.Builder()
                        .target(new LatLng(latitude, longitude))      // Sets the center of the map to Mountain View
                        .zoom(17)                   // Sets the zoom
                        .bearing(90)                // Sets the orientation of the camera to east
                        .tilt(30)                   // Sets the tilt of the camera to 30 degrees
                        .build();                   // Creates a CameraPosition from the builder
                mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    
                Intent intent = getIntent();
                intent.putExtra("latitude", latitude);
                intent.putExtra("longitude", longitude);
        //        LatLng sydney = new LatLng(latitude, longitude);
    
            }
        }
    
  • Rick Sanchez
    Rick Sanchez over 6 years
    That gets rid of the errors and the map activity displays but it is blank showing no features of the map