mapobject nullpointer exception

25 views
Skip to first unread message

howud...@gmail.com

unread,
Oct 5, 2016, 8:01:05 PM10/5/16
to CodenameOne Discussions
Simulator:no error
Android: crash with null point exception
iOS: crash with null pointer exception

re-using a mapobject causes a null pointer exception.   in the code below onRefresh() and onList() are dummy functions that simply change the icon of a map marker based upon user selection.  calling  mo.point.setIcon(image1);  results in the error because point is null;

Full code with most everything removed just enough to show the error

package com.howudodat.test.ui;

import com.codename1.googlemaps.MapContainer;
import com.codename1.googlemaps.MapContainer.MapObject;
import com.codename1.location.Location;
import com.codename1.location.LocationListener;
import com.codename1.location.LocationManager;
import com.codename1.maps.Coord;
import com.codename1.maps.MapListener;
import com.codename1.ui.Command;
import com.codename1.ui.Component;
import com.codename1.ui.Display;
import com.codename1.ui.EncodedImage;
import com.codename1.ui.FontImage;
import com.codename1.ui.Image;
import com.codename1.ui.Toolbar;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;

public class FrmMap extends com.codename1.ui.Form {
protected MapContainer cnt = new MapContainer();
protected MapObject mo = null;
protected Image image0 = FontImage.createMaterial(FontImage.MATERIAL_HOME, UIManager.getInstance().getComponentStyle("Command"), 5);
protected Image image1 = FontImage.createMaterial(FontImage.MATERIAL_REFRESH, UIManager.getInstance().getComponentStyle("Command"), 5);
protected Image image2 = FontImage.createMaterial(FontImage.MATERIAL_LIST, UIManager.getInstance().getComponentStyle("Command"), 5);
    public FrmMap() {
        this(Resources.getGlobalResources());
    }
    
    public FrmMap(Resources resourceObjectInstance) {
        initManualComponents();
Display.getInstance().callSerially(() -> { 
       centerMapToLocation();
});
    }
protected void initManualComponents() {
        setLayout(new BorderLayout());
        setTitle("Map");
        setName("FrmMap");
// sidebar
if (getToolbar() == null) {
setToolbar(new Toolbar());
}
FrmSideMenu.CreateToolbar(getToolbar());

this.setScrollable(false);
        this.addComponent(BorderLayout.CENTER, cnt);
        cnt.addMapListener(new MapListener() {
@Override
public void mapPositionUpdated(Component source, int zoom, Coord center) {
}
});
        
Command cmdRefresh = new Command("",FontImage.createMaterial(FontImage.MATERIAL_REFRESH, UIManager.getInstance().getComponentStyle("Command"), 5)) {
public void actionPerformed(com.codename1.ui.events.ActionEvent ev) {
onRefresh();
}
};
Command cmdList = new Command("",FontImage.createMaterial(FontImage.MATERIAL_LIST, UIManager.getInstance().getComponentStyle("Command"), 5)) {
public void actionPerformed(com.codename1.ui.events.ActionEvent ev) {
onList();
}
};
this.getToolbar().addCommandToRightBar(cmdRefresh);
this.getToolbar().addCommandToRightBar(cmdList);
    }
protected void centerMapToLocation() {
        LocationManager l = LocationManager.getLocationManager(); 
        l.setLocationListener(new LocationListener() {
        public void locationUpdated(Location loc) {     
        Coord c = new Coord(loc.getLatitude(), loc.getLongitude());
        cnt.zoom(c , 13);
        l.setLocationListener(null);
        onMapUpdate(13, c);
        }

        public void providerStateChanged(int newState) {
        }
        }); 
}

protected void onMapUpdate(int zoom, Coord center) {
mo = cnt.addMarker(EncodedImage.createFromImage(image0, false),
center,
"test",
"test",
new ActionListener<ActionEvent>() {
public void actionPerformed(ActionEvent evt) {
onClickMap();
}
}
);
}
protected void onRefresh() {
mo.point.setIcon(image1);
repaint();
}
protected void onList() {
mo.point.setIcon(image2);
repaint();
}
protected void onClickMap() {
System.out.println("whoohoo");
}
}


Shai Almog

unread,
Oct 5, 2016, 11:14:55 PM10/5/16
to CodenameOne Discussions, howud...@gmail.com
That's an internal API that's specifically hidden. I don't recall the exact implementation but if something is package protected it isn't meant for external usage and there is probably a reason....

Peter Carlson

unread,
Oct 5, 2016, 11:30:43 PM10/5/16
to Shai Almog, CodenameOne Discussions

So how can I change the icon? Remove the market and add another?

howud...@gmail.com

unread,
Oct 6, 2016, 7:55:18 PM10/6/16
to CodenameOne Discussions, shai....@gmail.com, howud...@gmail.com
I was able to get the behavior I wanted by clearing the maplayer and re-adding all the markers.  Is there a better way?

On Wednesday, October 5, 2016 at 8:30:43 PM UTC-7, Peter Carlson wrote:

So how can I change the icon? Remove the market and add another?

On Oct 5, 2016 8:14 PM,
That's an internal API that's specifically hidden. I don't recall the exact implementation but if something is package protected it isn't meant for external usage and there is probably a reason....

Shai Almog

unread,
Oct 6, 2016, 9:41:59 PM10/6/16
to CodenameOne Discussions, shai....@gmail.com, howud...@gmail.com
That's the only way.
We copy the state into the native space for iOS so modifying the image won't work.
Reply all
Reply to author
Forward
0 new messages