NullPointer with Primefaces GMap OverlaySelect event

3.2k Views Asked by At

I have a problem with Primefaces whose version is 3.0.M3 . I used gmap and prime p:ajax.I wanted to do something is to get clicked Marker when a marker is clicked by a user on Gmap.

Here is the Code

<p:tabView effect="slide" effectDuration="normal" style="width:1050px;height:450px" >
            <p:tab title="blabla">
                <h:panelGrid colums="1">    
                </h:panelGrid>
            </p:tab>
            <p:tab title="blabla" >
                <h:panelGrid colums="1">
                    <h:form id="mapId">

                        <p:gmap  id="asd" center="39.000409,35.201554" 
                            zoom="#{mapBean.modelMap.zoomLevel}" 
                            type="ROADMAP" 
                            style="width:1000px;height:400px"
                            model="#{mapBean.emptyModel}"
                            widgetVar="map" >

                            <p:ajax event="overlaySelect" listener="#{mapBean.onMarkerSelect}"/>

                        </p:gmap>

                    </h:form>   
                </h:panelGrid>
            </p:tab></p:tabView>

and My ManagedBean

public void onMarkerSelect(OverlaySelectEvent event) {  

    Marker marker = (Marker) event.getOverlay();
    if (marker!=null) {
        System.out.println(marker.getId());
    }
    System.out.println("Clicked");
    modelMap.setZoomLevel(modelMap.getZoomLevel()+1);

}

Managed Bean Declarations

@ManagedBean(name="mapBean")
@RequestScoped
public class MapBean implements Serializable 

I am taking NullPointerException in onMarkerSelect method.(event.getOverlay();)

5

There are 5 best solutions below

0
On

Just change your bean scope to @SessionScoped or @ViewScoped so your bean won't be initialized for each request.

2
On

Fixed my problem.

The problem was that when the MapModel was created it was a local var:

public MapModel getModel() {

    final MapModel mapModel = new DefaultMapModel(); // this should be a field

    final Set<MapEventDto> events = service.loadEvents();
    for (MapEventDto event : events) {

        final double latitude = event.getLatitude().doubleValue();
        final double longitude = event.getLongitude().doubleValue();
        final String magnitude = event.getMagnitude().toString();

        final String title = "Id: " + event.getId() + ", Lat: " + latitude + ", Lng: " + longitude + ", Mag: " + magnitude;

        mapModel.addOverlay(new Marker(new LatLng(latitude, longitude), title));

    }
    return mapModel;
}

The whole mapModel could be garbage collected after the map is rendered (as it is no longer needed). So when the overlay event is called there would be no mapModel any more.

As soon as I made mapModel a field of the Bean the problem disappeared.

0
On
final DefaultMapModel mapModel = new DefaultMapModel();

worked For as Global Variable,in viewScoped best vj

1
On

Use the static field as follows. It worked for me.

final static MapModel mapModel = new DefaultMapModel();
0
On

Got through this and solved by changing scope for the Bean to ViewScoped instead of Request Scope, so it lasts during user activity.

No final/static declarators needed

  @ManagedBean(name="mapBean")
  @ViewScoped
  public class MapBean implements Serializable {  
    private MapModel draggableModel;   
    ...
  }