NullPointer with GMap Add-in OverlaySelect

I have a problem with Primefaces with version 3.0.M3. I used gmap and prime p: ajax. I wanted to do something to click the marker when the user clicked the marker 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);

}

Bean Managed Ads

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

I accept NullPointerExceptionin the method onMarkerSelect. ( event.getOverlay();)

+5
source share
5 answers

The problem is fixed.

The problem was that when creating MapModelit 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;
}

All MapModelmay be garbage collected after the map is displayed (since it is no longer needed). Therefore, when an overlay event is triggered, it will no longer be MapModel.

MapModel Bean, .

+6

static . .

final static MapModel mapModel = new DefaultMapModel();
+3
final DefaultMapModel mapModel = new DefaultMapModel();

, in viewScoped VJ

0
source

Got this and decided by changing the scope of the Bean to ViewScopedinstead of the scope of the request, so it lasts during user activity.

No final / static declarators required

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

Just change the bean scope to @SessionScopedor @ViewScopedso that your bean is not initialized for each request.

0
source