Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointer with Primefaces GMap OverlaySelect event

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();)

like image 608
Cetin Imre Avatar asked Dec 15 '11 21:12

Cetin Imre


1 Answers

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.

like image 69
Simeon Avatar answered Oct 01 '22 03:10

Simeon