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.data.*;
import de.fhpotsdam.unfolding.mapdisplay.MapDisplayFactory;

import de.bezier.data.sql.*;

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

  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 (msql.next ())
    {
      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();
      events.add(event); 
      Location location = new Location(lat, lon);
    }
    println("Number of events: " + events.size());
  } else {
    //connection failed
  }
}

public void draw() {
  background(230);
  map1.draw();
  map2.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")) {
        fill(0); 
        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(AWTEDTExecutor.java:58)
            at jogamp.opengl.awt.AWTThreadingPlugin.invokeOnOpenGLThread(AWTThreadingPlugin.java:103)
            at jogamp.opengl.ThreadingImpl.invokeOnOpenGLThread(ThreadingImpl.java:206)
            at javax.media.opengl.Threading.invokeOnOpenGLThread(Threading.java:172)
            at javax.media.opengl.Threading.invoke(Threading.java:191)
            at javax.media.opengl.awt.GLCanvas.display(GLCanvas.java:541)
            at processing.opengl.PJOGL.requestDraw(PJOGL.java:688)
            at processing.opengl.PGraphicsOpenGL.requestDraw(PGraphicsOpenGL.java:1651)
            at processing.core.PApplet.run(PApplet.java:2256)
            at java.lang.Thread.run(Unknown Source)
        Caused by: java.lang.NullPointerException
            at map_instances.draw(map_instances.java:146)
            at processing.core.PApplet.handleDraw(PApplet.java:2386)
            at processing.opengl.PJOGL$PGLListener.display(PJOGL.java:862)
            at jogamp.opengl.GLDrawableHelper.displayImpl(GLDrawableHelper.java:665)
            at jogamp.opengl.GLDrawableHelper.display(GLDrawableHelper.java:649)
            at javax.media.opengl.awt.GLCanvas$10.run(GLCanvas.java:1289)
            at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:1119)
            at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:994)
            at javax.media.opengl.awt.GLCanvas$11.run(GLCanvas.java:1300)
            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$3.run(Unknown Source)
            at java.awt.EventQueue$3.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.security.ProtectionDomain$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 java.awt.EventDispatchThread.run(Unknown Source)

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

Tagged:

Answers

  • 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.