Map points in different colours based on datatype

Hi, I've read data from a mySQL database and I'm trying to colour markers by locationType. This is the code I have so far, but I get an error when I use the for loop with colours in the draw section:

import de.fhpotsdam.unfolding.*;
import de.fhpotsdam.unfolding.geo.*;
import de.fhpotsdam.unfolding.utils.*;
import de.fhpotsdam.unfolding.providers.*;
import de.fhpotsdam.unfolding.mapdisplay.MapDisplayFactory;


UnfoldingMap map1;
UnfoldingMap map2;
MySQL msql;

ArrayList<Events> events = new ArrayList(); 

int map1X = 250;
int map2X = 775;
int mapY = 10;
int mapW = 515;
int mapH = 630;

Location aLocation = new Location(-37.7f, 144.8f);
Location bLocation = new Location(-37.8f, 145.1f);

class Events {

  int participant_id;
  String locationType;
  double lat;
  double lon;
  Location location;

public void setup() {
  size(1300, 650, P2D);

  map1 = new UnfoldingMap(this, "map1", map1X, mapY, mapW, mapH, true, false, new Google.GoogleTerrainProvider());
  map2 = new UnfoldingMap(this, "map2", map2X, mapY, mapW, mapH, true, false, new Google.GoogleTerrainProvider());
  MapUtils.createDefaultEventDispatcher(this, map1, map2);
  map1.zoomAndPanTo(aLocation, 11);
  map2.zoomAndPanTo(bLocation, 11);
  float maxPanningDistance = 50;   // in km
  map1.setPanningRestriction(humLocation, maxPanningDistance);
  map2.setPanningRestriction(borLocation, maxPanningDistance);

  //connect to MySQL database
  String user = "****";
  String pass = "****";
  String database = "database";
  msql = new MySQL(this, "localhost", database, user, pass);

  if (msql.connect())
  { //Events
    msql.query("select ... etc;");
    while ( ())
      String l = msql.getString("locationType");
      int id = msql.getInt("participant_id");
      float lat = msql.getFloat("lat"); 
      float lon = msql.getFloat("lon");
      println("Events: " + l + "   " + id + "   " + lat + "   " + lon);

      Events event = new Events();
      Location location = new Location(lat, lon);
    println("Number of events: " + events.size());
  } else {
    //connection failed

public void draw() {

  for (Events event : events) {
    ScreenPosition pos = map1.getScreenPosition(event.location);

    if ( pos.x > map2X  && pos.x < map2X + mapW &&
      pos.y > map2Y  && pos.y < map2Y + mapH  ) { 

      // Draw circles for events
      if (event.locationType.equals( "1")) {
        ellipse(pos.x, pos.y, 8, 8);
      } else if (event.locationType.equals( "2")) {
        fill(255, 0, 0); 
        ellipse(pos.x, pos.y, 8, 8);
      } else if (event.locationType.equals(  "3")) {
        fill(255, 50, 0); 
        ellipse(pos.x, pos.y, 8, 8);

When I run this, I get an error (due to the if loop for fill in draw):

        java.lang.RuntimeException: java.lang.NullPointerException
            at com.jogamp.common.util.awt.AWTEDTExecutor.invoke(
            at jogamp.opengl.awt.AWTThreadingPlugin.invokeOnOpenGLThread(
            at jogamp.opengl.ThreadingImpl.invokeOnOpenGLThread(
            at processing.opengl.PJOGL.requestDraw(
            at processing.opengl.PGraphicsOpenGL.requestDraw(
            at Source)
        Caused by: java.lang.NullPointerException
            at map_instances.draw(
            at processing.core.PApplet.handleDraw(
            at processing.opengl.PJOGL$PGLListener.display(
            at jogamp.opengl.GLDrawableHelper.displayImpl(
            at jogamp.opengl.GLDrawableHelper.display(
            at jogamp.opengl.GLDrawableHelper.invokeGLImpl(
            at jogamp.opengl.GLDrawableHelper.invokeGL(
            at java.awt.event.InvocationEvent.dispatch(Unknown Source)
            at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
            at java.awt.EventQueue.access$200(Unknown Source)
            at java.awt.EventQueue$ Source)
            at java.awt.EventQueue$ Source)
            at Method)
            at$1.doIntersectionPrivilege(Unknown Source)
            at java.awt.EventQueue.dispatchEvent(Unknown Source)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
            at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
            at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
            at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
            at Source)

What have I done wrong?? Thanks in advance for any help



  • what does line 68 do?

  • it combines the latitude and longitude into one "location" for each point to display

  • But you do nothing with the result.

  • Line 82.

  • Answer ✓

    We cannot run your program, so you should indicate at which line you get this error. The line should be highlighted in the PDE when the program stops.

    Note: I would name your class Event, since it refers to a single event.

    Looking deeper in the program, you instantiate an Event(s) object, but its fields have defaults values: null for objects, zero for numerical fields.
    So when you try and access the location (or locationType), you get the NPE.

    In other words, you read data from the SQL database, you create events, but you forgot to connect both.

  • Thanks for the advice. I have solved the problem, which was in lines 88-96. It was due to syntax errors.

Sign In or Register to comment.