0

I have a very simple example of an instance for creating a IconGenerator and make it visible on the map.

The problem is that it gives me a NullPointerException each time it wants to create the IconGenerator iconita = new IconGenerator(getContext());

public class Controller {
GoogleMap mMap;
Context context;
private static Controller instance;

private Controller() {
}

public static Controller getInstance() {
    if (instance == null) {
        instance = new Controller();
    }
    return instance;
}

public void setContext() {
    this.context = context;
}

public Context getContext() {
    return context;
}

public Map<String, List<Marker>> adaugaRuta(ShowTheRute theRoute) {
    Map<String, List<Marker>> objectMap = new HashMap<>();

    IconGenerator iconita = new IconGenerator(getContext()); <-- HERE it gives NullPointerException
    iconita.setContentPadding(5, -5, 5, -5);
    iconita.setRotation(-270);
    iconita.setContentRotation(270);

    Bitmap iconbit = iconita.makeIcon("Marker 1.");
    final Marker mae = mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(iconbit)).position(new LatLng(46.762035, 23.597659)).anchor(0f, 0.5f));
    mae.isVisible();

    List<Marker> marker1 = new ArrayList<>();
    marker1.add(mae);

    objectMap.put("marker1", marker1);

    theRoute.getTheRute(objectMap);
    return objectMap;
}

}

1 Answers1

1

Your context object is never set, you have a mistake in the setter of the context

public void setContext() {
    this.context = context;
}

you are actually rewriting the context with itself.. it should be:

public void setContext(Context context) {
    this.context = context;
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • i rewrite as you said, but still null pointer at the same line :( –  Feb 03 '16 at 19:26
  • yes for sure, the class that is using the Controler object needs to set the context before calling the adaugaRuta method – ΦXocę 웃 Пepeúpa ツ Feb 03 '16 at 19:27
  • can you please be more specific in what to do, i am a begginer. I called in the main activity this: Controller.getinstance().adaugaRuta(MapsActivity.this) –  Feb 03 '16 at 20:07
  • 1
    i resolved the problem. The thing was that the IconGenerator needs the context from the main activity which uses FragmentActivity so the map can be generated otherwise is always null from the singleton context. Thank you for the idea :) –  Feb 03 '16 at 20:35