user268397
user268397

Reputation: 1927

Android - NullPointerException

I'm getting a NullPointerException that is breaking the code on this line:

if (options.isVisible()) {

Here is the block of code:

protected static GoogleMap map;
protected static MarkerOptions markerOptions;
private OnMarkerCreateListener listener;

public LazyMarker(GoogleMap map, MarkerOptions options) {
    this(map, options, null);
}

public LazyMarker(GoogleMap map, MarkerOptions options, OnMarkerCreateListener listener) {
    if (options.isVisible()) {
        createMarker(map, options, listener);
    } else {
        this.map = map;
        this.markerOptions = copy(options);
        this.listener = listener;
    }
}

I'm guessing it has to do with the MarkerOptions variable but I'm not sure if it's breaking because it is not being initialized? Any help would greatly be appreciated.

Upvotes: 0

Views: 286

Answers (3)

Suresh Atta
Suresh Atta

Reputation: 122026

As @Mzn said Pass a new MarkerOptions() and

You should do a null check there

if (options!=null &&options.isVisible()) {

Upvotes: 1

Henrik Söderlund
Henrik Söderlund

Reputation: 103

If you could tell us where you are calling LazyMarker() from and how it looks inside that class, it would be great. I would suggest that you surround the call with a try-catch.

try {
    ...
    marker = new LazyMarker(map, options, listener);
    ...
} catch (NullPointerException e) {
    e.printStackTrace();
}

If this still not works, be sure to check that you are actually defining a MarkerOptions object to the variable.

Hope it helps.

Upvotes: 0

Lzh
Lzh

Reputation: 3635

Yes. The runtime told you (by throwing an exception) and now you just got human confirmation. You can initialize MarkerOptions with an empty constructor. Pass a new MarkerOptions() if you don't need to make any customization to the default marker.

See this link for documentation: https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/model/MarkerOptions

Upvotes: 0

Related Questions