Search code examples
javarepast-simphony

Repast: unknown errors reported


Issue 1:

Below is the error I received but I have no idea where is wrong.

Caused by: java.lang.NullPointerException
    at repast.simphony.visualizationOGL2D.DisplayOGL2D.getSpatialForObject(DisplayOGL2D.java:535)
    at repast.simphony.visualizationOGL2D.NetworkLayerOGL2D.update(NetworkLayerOGL2D.java:103)
    at repast.simphony.visualizationOGL2D.DisplayOGL2D.update(DisplayOGL2D.java:410)
    at repast.simphony.visualizationOGL2D.DisplayOGL2D.reshape(DisplayOGL2D.java:589)
    at saf.v3d.Canvas2D.reshape(Canvas2D.java:400)
    at jogamp.opengl.GLDrawableHelper.reshape(GLDrawableHelper.java:742)
    at jogamp.opengl.GLDrawableHelper.reshape(GLDrawableHelper.java:748)
    at javax.media.opengl.awt.GLJPanel$Updater.display(GLJPanel.java:1404)
    at javax.media.opengl.awt.GLJPanel$9.run(GLJPanel.java:1483)
    at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:1277)
    ... 69 more

This is the input file used to generate the edge network:

enter image description here

below is the part of the codes adding the route network which reported above issues. I am trying to map an undirected route network to ensure there is only one edge connecting two hub. To avoid duplication I have a if condition (if (net.getEdge(source, target) == null)) to check if there is already an edge between the two hubs. if no, create a new one, if yes, do nothing. If I remove this if condition, there is no errors but there are edge duplication. If I add such if condition, I will get below errors every time. I don't know why? But if I remove the display completely in GUI, it works fine to omit all duplication.

//      add route network
        Network<Object> net = (Network<Object>)context.getProjection("IntraCity Network");
        IndexedIterable<Object> local_hubs = context.getObjects(LocalHub.class);
        for (int i = 0; i <= CSV_reader_route.getMaster().size() - 1; i++) {
            String source = (String) CSV_reader_route.getMaster().get(i).get(0);
            String target = (String) CSV_reader_route.getMaster().get(i).get(3);
            double dist = Double.parseDouble((String) CSV_reader_route.getMaster().get(i).get(6));
            double time = Double.parseDouble((String) CSV_reader_route.getMaster().get(i).get(7));

            Object source_hub = null;
            Object target_hub = null;
            Query<Object> source_query = new PropertyEquals<Object>(context, "hub_code", source);
            for (Object o : source_query.query()) {
                if (o instanceof LocalHub) {
                    source_hub = (LocalHub) o;
                }
                if (o instanceof GatewayHub) {
                    source_hub = (GatewayHub) o;
                }
            }

            Query<Object> target_query = new PropertyEquals<Object>(context, "hub_code", target);
            for (Object o : target_query.query()) {
                if (o instanceof LocalHub) {
                    target_hub = (LocalHub) o;
                }
                if (o instanceof GatewayHub) {
                    target_hub = (GatewayHub) o;
                }
            }

//          System.out.println(target_hub.getClass() + " " + time);
//          Route this_route = (Route) net.addEdge(source_hub, target_hub);
//          context.add(this_route);
//          System.out.println(net.getEdge(source_hub, target_hub));
            if (net.getEdge(source_hub, target_hub) == null) {
                Route this_route = (Route) net.addEdge(source_hub, target_hub);
                context.add(this_route);
//              this_route.setDist(dist);
//              this_route.setTime(time); }
            }



        }

Problem found for issue 1:

I found why the issue happens, it should be source_hub and target_hub in net.getEdge(source, target).

        if (net.getEdge(source, target) == null) {
            Route this_route = (Route) net.addEdge(source, target);

Issue 2:

UPDATE: I have found it's very much slow to initialize this part of the code. IT TAKES 40 SECONDS TO FINISH! where is problem of the codes? I tested and found that my CSV reading code can actually finish the reading work in less than 1 second if I run it separately. This DataReader is a part used by above process to initialize the relevant properties.

Below is the DataReader codes used by above route generation process. However,I suspect it's the query loop in above route generation codes which consumes much of the time for initialization:

public class DataReader {

    private String csvFile;
    private List<String> sub = new ArrayList<String>();
    private List<List> master = new ArrayList<List>();


    public void ReadFromCSV(String csvFile) {

        String line = "";
        String cvsSplitBy = ",";

        try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
            System.out.println("Header " + br.readLine());
            while ((line = br.readLine()) != null) {

                // use comma as separator
                String[] list = line.split(cvsSplitBy);
//                System.out.println("the size is " + country[1]);
                for (int i = 0; i < list.length; i++) {
                    sub.add(list[i]);
                }
                List<String> temp = (List<String>) ((ArrayList<String>) sub).clone();
//                master.add(new ArrayList<String>(sub));
                master.add(temp);
                sub.removeAll(sub);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(master);
    }

    public List<List> getMaster() {
        return master;
    }

}

Solution

  • You can get this error if you haven't specified a style for that agent type. For example, if you introduced a new agent type to the model and instantiated agents of that type, but hadn't specified a style for that type in the display wizard.