0

EDIT: I have initialised the BreakDownOnMaps object now

I'm getting the following error:

E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference
E/AndroidRuntime:     at com.example.yomac_000.rsrpechhulp.BreakDownOnMaps.handleNewLocation(BreakDownOnMaps.java:57)
E/AndroidRuntime:     at utils.MyLocationListener.onConnected(MyLocationListener.java:52)

And it's because of this line of code:

gMap.addMarker(options);

And this of line of code:

breakDownOnMaps.handleNewLocation(location);

Below you can see my code:

MyLocationListener.java:

public class MyLocationListener implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationListener {
    private final Context context;
    private GoogleMap mMap;
    private BreakDownOnMaps breakDownOnMaps = new BreakDownOnMaps();
    protected GoogleApiClient mGoogleApiClient;
    private LocationRequest mLocationRequest =  new LocationRequest();

    public MyLocationListener(Context context) {
        this.context = context;
        buildApi();
    }

    @Override
    public void onLocationChanged(Location location) {
        breakDownOnMaps.handleNewLocation(location);
    }

    @Override
    public void onConnected(Bundle bundle) {
        System.out.println("onConnected");
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        }
        Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (location == null) {
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, (com.google.android.gms.location.LocationListener) this);
        }
        else {
            breakDownOnMaps.handleNewLocation(location);
        };
    }

    private void buildApi() {
        mGoogleApiClient = new GoogleApiClient.Builder(context)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    public void connect() {
        mGoogleApiClient.connect();
    }
}

And here is the code of BreakDownOnMaps.java:

public class BreakDownOnMaps extends FragmentActivity implements
        OnMapReadyCallback {
    double currentLatitude;
    double currentLongitude;
    LatLng latLng;
    GoogleMap gMap;
    private MyLocationListener myLocationListener;

    @TargetApi(Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_break_down_on_maps);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        myLocationListener = new MyLocationListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        myLocationListener.connect();
    }

    @Override
    protected void onResume() {
        super.onResume();
        myLocationListener.connect();
    }

    public void handleNewLocation(Location loc) {
        currentLatitude = loc.getLatitude();
        currentLongitude = loc.getLongitude();
        latLng = new LatLng(currentLatitude, currentLongitude);
        System.out.println("handleNewLocation ");
        MarkerOptions options = new MarkerOptions()
                .position(latLng)
                .title("I am here!");
        gMap.addMarker(options);
        gMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        System.out.println("currentLatitude : " + currentLatitude);
        System.out.println("currentLongitude : " + currentLongitude);
        latLng = new LatLng(currentLatitude, currentLongitude);
        setgMap(googleMap);
        if(currentLatitude != 0 || currentLongitude != 0) {
            MarkerOptions options = new MarkerOptions()
                    .position(latLng)
                    .title("I am here!");
            googleMap.addMarker(options);
            googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        }
    }

    public void setgMap(GoogleMap gMap) {
        this.gMap = gMap;
    }
}

Before refactoring I had put all of my code in BreakDownOnMaps.java. And everything did worked fine. So if you want to see how it looks before you can see it through the link below:

Link: http://pastebin.com/p6c3UeSC

I had also heared that the art of refactoring is not adding code, but only to change the code in the way it looks like. So I really don't know what I'm doing wrong.

superkytoz
  • 1,267
  • 4
  • 23
  • 43
  • 2
    In `MyLocationListener` where is `breakDownOnMaps` set? – Knossos Jan 19 '16 at 14:58
  • It might be possible that you haven't initialised breakDownOnMaps. – sri Jan 19 '16 at 15:05
  • @Knossos I have edited my question and the error log. And now I have initialised my BreakDownOnMaps object. But I'm still getting an null object error. As you can see I have clearly initialised my GoogleMap object in the `onMapReady()` method. – superkytoz Jan 19 '16 at 15:43
  • The error is now on `mMap`. Please look at the link in the comment above. You need to understand what a Null Pointer Exception is, to be able to fix it when it next appears. – Knossos Jan 19 '16 at 15:46
  • @Knossos Yes I know that already. But I'm using the `getMapAsync()` method to ensure that I'm not getting a `null` GoogleMap object. According to Google: "Use getMapAsync(OnMapReadyCallback) instead. The callback method provides you with a GoogleMap instance guaranteed to be non-null and ready to be used." Source link: https://developers.google.com/android/reference/com/google/android/gms/maps/SupportMapFragment#getMapAsync(com.google.android.gms.maps.OnMapReadyCallback) – superkytoz Jan 19 '16 at 16:10

0 Answers0