0

I created Disease Spread Simulator with Repast 2.7. When I try press Start Run I'm getting this error :

java.lang.NullPointerException
    at repast.simphony.visualization.engine.DisplayProducer.setContext(DisplayProducer.java:35)
    at repast.simphony.visualization.engine.DisplayProducer.<init>(DisplayProducer.java:28)
    at repast.simphony.visualization.engine.DisplayComponentControllerAction.runInitialize(DisplayComponentControllerAction.java:113)
    at repast.simphony.engine.controller.DefaultController$2.visit(DefaultController.java:214)
    at repast.simphony.engine.controller.DefaultController$2.visit(DefaultController.java:1)
    at repast.simphony.util.collections.NaryTree.preOrderTraverals(NaryTree.java:292)
    at repast.simphony.util.collections.NaryTree.preOrderTraverals(NaryTree.java:295)
    at repast.simphony.util.collections.NaryTree.preOrderTraverals(NaryTree.java:295)
    at repast.simphony.util.collections.NaryTree.preOrderTraversal(NaryTree.java:288)
    at repast.simphony.engine.controller.DefaultController.runInitialize(DefaultController.java:212)
    at repast.simphony.engine.controller.DefaultController.runInitialize(DefaultController.java:383)
    at repast.simphony.ui.RSApplication.initSim(RSApplication.java:157)
    at repast.simphony.ui.RSApplication.start(RSApplication.java:189)
    at repast.simphony.ui.action.StartRun.actionPerformed(StartRun.java:17)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
    at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
    at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
    at java.desktop/java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:297)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6632)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
    at java.desktop/java.awt.Component.processEvent(Component.java:6397)
    at java.desktop/java.awt.Container.processEvent(Container.java:2263)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5008)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2772)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

I checked all the codes 3 - 4 times and I did a lot of research on the internet but I could not find out why the problem was caused. Here my codes:

all my project : https://drive.google.com/drive/folders/1jFfUmBZEznCTASUJ0D6QoPv-p62yUbin

Here My Classes :

Healthy Agent Class :


import java.util.List;

import repast.simphony.engine.environment.RunEnvironment;
import repast.simphony.engine.schedule.ScheduledMethod;
import repast.simphony.parameter.Parameters;
import repast.simphony.query.space.grid.GridCell;
import repast.simphony.query.space.grid.GridCellNgh;
import repast.simphony.random.RandomHelper;
import repast.simphony.space.SpatialMath;
import repast.simphony.space.continuous.ContinuousSpace;
import repast.simphony.space.continuous.NdPoint;
import repast.simphony.space.grid.Grid;
import repast.simphony.space.grid.GridPoint;
import repast.simphony.util.SimUtilities;



public class Healthy {
    private ContinuousSpace<Object> space;
    private Grid<Object> grid;
    Parameters params = RunEnvironment.getInstance().getParameters();
    double chance_to_infect ;
    private double prob_to_social_isolate = 0.65 ;
    boolean social_isolate  ;   


    public Healthy(ContinuousSpace<Object> space, Grid<Object> grid) {
        this.space = space;
        this.grid = grid;
        this.chance_to_infect = 0.785 ;
        this.social_isolate = true ;

        if(Math.random() <= prob_to_social_isolate) {
            this.social_isolate = false;
        }


    }

    @ScheduledMethod(start = 1, interval = 1) 
    public void step() {
        //if(Math.random() <= prob_to_social_isolation)
        if(this.social_isolate) {
            return;
        }
        // get the grid  location of this Human 
        GridPoint pt = grid.getLocation(this);
        // use the GridCellNg class to create GridCell for
        // the surronding neighborgood.

        GridCellNgh<Object> nghCreator = new GridCellNgh<Object> ( grid, pt, Object.class, 1, 1);
        List<GridCell<Object>> gridCells = nghCreator.getNeighborhood(true); 
        SimUtilities.shuffle(gridCells, RandomHelper.getUniform());


        GridCell<Object> cell = gridCells.get(0);

        GridPoint point_to_move = cell.getPoint();


    }

    public void moveTowards (GridPoint pt) {
        // only move if we are not already in this grid location 
        if(!pt.equals(grid.getLocation(this))) {
            NdPoint myPoint = space.getLocation(this);
            NdPoint otherPoint = new NdPoint(pt.getX(), pt.getY());
            double angle = SpatialMath.calcAngleFor2DMovement(space, myPoint, 
                    otherPoint);
            space.moveByVector( this, 2, angle, 0);
            myPoint = space.getLocation(this);
            grid.moveTo(this, (int) myPoint.getX(), (int) myPoint.getY());

        }
    }






}

Dead Agent Class :


import repast.simphony.space.continuous.ContinuousSpace;
import repast.simphony.space.grid.Grid;

public class Dead {

    private ContinuousSpace<Object> space;
    private Grid<Object> grid;

public Dead (ContinuousSpace<Object> space, Grid<Object> grid) {

        this.space = space;
        this.grid = grid;



        }

}

Hospitalized Agent Class :


import java.util.ArrayList;
import java.util.List;

import repast.simphony.context.Context;
import repast.simphony.engine.schedule.ScheduledMethod;
import repast.simphony.query.space.grid.GridCell;
import repast.simphony.query.space.grid.GridCellNgh;
import repast.simphony.random.RandomHelper;
import repast.simphony.space.SpatialMath;
import repast.simphony.space.continuous.ContinuousSpace;
import repast.simphony.space.continuous.NdPoint;
import repast.simphony.space.graph.Network;
import repast.simphony.space.grid.Grid;
import repast.simphony.space.grid.GridPoint;
import repast.simphony.util.ContextUtils;
import repast.simphony.util.SimUtilities;

public class Hospitalized {

    private ContinuousSpace<Object> space;
    private Grid<Object> grid;
    double prob_recover ;


public Hospitalized (ContinuousSpace<Object> space, Grid<Object> grid) {
    this.space = space;
    this.grid = grid;
    this.prob_recover = 0.8 ;


        }

@ScheduledMethod(start = 1, interval = 1) 
public void step() {
    //if(Math.random() <= prob_to_social_isolation)
    // get the grid  location of this Human 
    GridPoint pt = grid.getLocation(this);
    // use the GridCellNg class to create GridCell for
    // the surronding neighborgood.

    GridCellNgh<Object> nghCreator = new GridCellNgh<Object> ( grid, pt, Object.class, 1, 1);
    List<GridCell<Object>> gridCells = nghCreator.getNeighborhood(true); 
    SimUtilities.shuffle(gridCells, RandomHelper.getUniform());


    GridCell<Object> cell = gridCells.get(0);

    GridPoint point_to_move = cell.getPoint();


}

public void moveTowards (GridPoint pt) {
    // only move if we are not already in this grid location 
    if(!pt.equals(grid.getLocation(this))) {
        NdPoint myPoint = space.getLocation(this);
        NdPoint otherPoint = new NdPoint(pt.getX(), pt.getY());
        double angle = SpatialMath.calcAngleFor2DMovement(space, myPoint, 
                otherPoint);
        space.moveByVector( this, 2, angle, 0);
        myPoint = space.getLocation(this);
        grid.moveTo(this, (int) myPoint.getX(), (int) myPoint.getY());

    }
}

public void dead() {

    GridPoint pt = grid.getLocation(this);
    List<Object> hospitalized = new ArrayList<Object>();
    //Get all infected at the new location

      for (Object obj : grid.getObjectsAt(pt.getX(), pt.getY())) {
           if(obj instanceof Hospitalized){
               hospitalized.add(obj);
           }
       }

      if(hospitalized.size() > 0) {
           for(Object obj : hospitalized ) {
               double random = Math.random();
               if(random >= ((Hospitalized) obj).prob_recover){

                   NdPoint spacePt = space.getLocation(obj);
                   Context<Object> context = ContextUtils.getContext(obj);
                   context.remove(obj);
                   Dead dead = new Dead(space, grid);
                   context.add(dead);
                   space.moveTo(dead, spacePt.getX(), spacePt.getY());
                   grid.moveTo(dead, pt.getX(), pt.getY());

                   Network<Object> net = (Network<Object>) context.getProjection("infection network");
                   net.addEdge(this, dead);


               }
           }
       }

}

}

Infected Agent Class :


import java.util.ArrayList;
import java.util.List;

import repast.simphony.context.Context;
import repast.simphony.engine.schedule.ScheduledMethod;
import repast.simphony.query.space.grid.GridCell;
import repast.simphony.query.space.grid.GridCellNgh;
import repast.simphony.random.RandomHelper;
import repast.simphony.space.SpatialMath;
import repast.simphony.space.continuous.ContinuousSpace;
import repast.simphony.space.continuous.NdPoint;
import repast.simphony.space.graph.Network;
import repast.simphony.space.grid.Grid;
import repast.simphony.space.grid.GridPoint;
import repast.simphony.util.ContextUtils;
import repast.simphony.util.SimUtilities;




public class Infected {

    private ContinuousSpace<Object> space;
    private Grid<Object> grid;
    private int days_infected ;
    double prob_to_go_to_hospital ;
    double prob_recover_without_hospital ;
    private boolean hospitalized;

    public Hospital hospital;

    public Infected (ContinuousSpace<Object> space, Grid<Object> grid) {

        this.space = space;
        this.grid = grid;
        this.days_infected = ((int) Math.random()*14);
        this.prob_to_go_to_hospital = 0.875;
        this.prob_recover_without_hospital = 0.2 ;
        this.hospitalized = false;
        this.hospital = null;

        }

    @ScheduledMethod(start = 1, interval = 1) 
    public void step() {
        // get the grid  location of this Human 
        GridPoint pt = grid.getLocation(this);
        // use the GridCellNg class to create GridCell for
        // the surronding neighborgood.

        GridCellNgh<Object> nghCreator = new GridCellNgh<Object> ( grid, pt, Object.class, 1, 1);
        List<GridCell<Object>> gridCells = nghCreator.getNeighborhood(true); 
        SimUtilities.shuffle(gridCells, RandomHelper.getUniform());




        GridCell<Object> cell = gridCells.get(0);

        GridPoint point_to_move = cell.getPoint();


    }

    public void moveTowards (GridPoint pt) {
        // only move if we are not already in this grid location 
        if(!pt.equals(grid.getLocation(this))) {
            NdPoint myPoint = space.getLocation(this);
            NdPoint otherPoint = new NdPoint(pt.getX(), pt.getY());
            double angle = SpatialMath.calcAngleFor2DMovement(space, myPoint, 
                    otherPoint);
            space.moveByVector( this, 2, angle, 0);
            myPoint = space.getLocation(this);
            grid.moveTo(this, (int) myPoint.getX(), (int) myPoint.getY());

        }
    }

    public void infect(){

        GridPoint pt = grid.getLocation(this);
        List<Object> healthy = new ArrayList<Object>();
        //Get all healthys at the new location


       for (Object obj : grid.getObjectsAt(pt.getX(), pt.getY())) {
           if(obj instanceof Healthy){
               healthy.add(obj);
           }
       }


       //infect any random healthy
       if(healthy.size() > 0) {
           for(Object obj : healthy) {
               double random = Math.random();
               if(random <= ((Healthy) obj).chance_to_infect &&  !((Healthy) obj).social_isolate ){

                   NdPoint spacePt = space.getLocation(obj);
                   Context<Object> context = ContextUtils.getContext(obj);
                   context.remove(obj);
                   Infected infected = new Infected(space, grid);
                   context.add(infected);
                   space.moveTo(infected, spacePt.getX(), spacePt.getY());
                   grid.moveTo(infected, pt.getX(), pt.getY());

                   Network<Object> net = (Network<Object>) context.getProjection("infection network");
                   net.addEdge(this, infected);


               }
           }
       }
    }

    public void hospitalized() {


        GridPoint pt = grid.getLocation(this);
        List<Object> infected = new ArrayList<Object>();
        //Get all infected at the new location

          for (Object obj : grid.getObjectsAt(pt.getX(), pt.getY())) {
               if(obj instanceof Infected){
                   infected.add(obj);
               }
           }

          if(infected.size() > 0) {
               for(Object obj : infected ) {                   
                   if(((Infected) obj).hospitalized == true ){

                       NdPoint spacePt = space.getLocation(obj);
                       Context<Object> context = ContextUtils.getContext(obj);
                       context.remove(obj);
                       Hospitalized hospitalized = new Hospitalized(space, grid);
                       context.add(hospitalized);
                       space.moveTo(hospitalized, spacePt.getX(), spacePt.getY());
                       grid.moveTo(hospitalized, pt.getX(), pt.getY());

                       Network<Object> net = (Network<Object>) context.getProjection("infection network");
                       net.addEdge(this, hospitalized);


                   }
               }
           }



    }

    public void dead () {

        GridPoint pt = grid.getLocation(this);
        List<Object> infected = new ArrayList<Object>();
        //Get all infected at the new location

          for (Object obj : grid.getObjectsAt(pt.getX(), pt.getY())) {
               if(obj instanceof Infected){
                   infected.add(obj);
               }
           }

          if(infected.size() > 0) {
               for(Object obj : infected ) {
                   double random = Math.random();
                   if(((Infected) obj).hospitalized == false && days_infected > 15 && 
                        random >= ((Infected) obj).prob_recover_without_hospital){

                       NdPoint spacePt = space.getLocation(obj);
                       Context<Object> context = ContextUtils.getContext(obj);
                       context.remove(obj);
                       Dead dead = new Dead(space, grid);
                       context.add(dead);
                       space.moveTo(dead, spacePt.getX(), spacePt.getY());
                       grid.moveTo(dead, pt.getX(), pt.getY());

                       Network<Object> net = (Network<Object>) context.getProjection("infection network");
                       net.addEdge(this, dead);


                   }
               }
           }



    }

    public Hospital getNearestHospital() {
           double minDistSq = Double.POSITIVE_INFINITY ;
           Hospital minAgent = null ; 
           NdPoint myLocation ; 
           Context context = ContextUtils.getContext(this);

           for(Object agent: context) {
               if(agent instanceof Hospital) {
                   Hospital thishospital = (Hospital) agent;
                   if(thishospital.current_capacity > 0) {
                       NdPoint currloc = space.getLocation(this);
                       NdPoint loc = space.getLocation(agent);
                       double distSq = ((currloc.getX() - loc.getX())*(currloc.getX() - loc.getX()))+((currloc.getY() - loc.getY())*(currloc.getY() - loc.getY()));
                       if (distSq < minDistSq) {
                           minDistSq = distSq;
                           minAgent = (Hospital) agent;

                       }
                   }
               }
           }

               minAgent.current_capacity --;
               return minAgent;



       }

    private void go_to_hospital() {


           if(Math.random() < prob_to_go_to_hospital) {
               //Write code to get nearest hospital
               //send agent there 
               Hospital nearest_hospital = getNearestHospital();
               if(nearest_hospital == null) {
                   return;
               }
               NdPoint target_location = space.getLocation(nearest_hospital);
               space.moveTo(this, (double)target_location.getX(), (double)target_location.getY());
               grid.moveTo(this, (int) target_location.getX(), (int) target_location.getY());
               this.hospital = nearest_hospital ;
               hospitalized = true ;


           }


       }




    }


Recovered Agent Class :


import java.util.List;

import repast.simphony.engine.environment.RunEnvironment;
import repast.simphony.engine.schedule.ScheduledMethod;
import repast.simphony.parameter.Parameters;
import repast.simphony.query.space.grid.GridCell;
import repast.simphony.query.space.grid.GridCellNgh;
import repast.simphony.random.RandomHelper;
import repast.simphony.space.SpatialMath;
import repast.simphony.space.continuous.ContinuousSpace;
import repast.simphony.space.continuous.NdPoint;
import repast.simphony.space.grid.Grid;
import repast.simphony.space.grid.GridPoint;
import repast.simphony.util.SimUtilities;



public class Recovered{
    private ContinuousSpace<Object> space;
    private Grid<Object> grid;



    public Recovered(ContinuousSpace<Object> space, Grid<Object> grid) {
        this.space = space;
        this.grid = grid;





    }

    @ScheduledMethod(start = 1, interval = 1) 
    public void step() {


        GridPoint pt = grid.getLocation(this);


        GridCellNgh<Object> nghCreator = new GridCellNgh<Object> ( grid, pt, Object.class, 1, 1);
        List<GridCell<Object>> gridCells = nghCreator.getNeighborhood(true); 
        SimUtilities.shuffle(gridCells, RandomHelper.getUniform());


        GridCell<Object> cell = gridCells.get(0);

        GridPoint point_to_move = cell.getPoint();


    }

    public void moveTowards (GridPoint pt) {
        // only move if we are not already in this grid location 
        if(!pt.equals(grid.getLocation(this))) {
            NdPoint myPoint = space.getLocation(this);
            NdPoint otherPoint = new NdPoint(pt.getX(), pt.getY());
            double angle = SpatialMath.calcAngleFor2DMovement(space, myPoint, 
                    otherPoint);
            space.moveByVector( this, 2, angle, 0);
            myPoint = space.getLocation(this);
            grid.moveTo(this, (int) myPoint.getX(), (int) myPoint.getY());

        }
    }




Hospital Agent Class :


import repast.simphony.engine.environment.RunEnvironment;
import repast.simphony.parameter.Parameters;
import repast.simphony.space.continuous.ContinuousSpace;
import repast.simphony.space.grid.Grid;

public class Hospital {
    Parameters params = RunEnvironment.getInstance().getParameters();
    private int number_of_rooms = 10;
    public int current_capacity;
    private ContinuousSpace<Object> space;
    private Grid<Object> grid;

    public Hospital (ContinuousSpace<Object> space, Grid<Object> grid) {
        this.space = space;
        this.grid = grid;
        this.current_capacity = number_of_rooms;

    }


}

Builder Class :


import repast.simphony.context.Context;
import repast.simphony.context.DefaultContext;
import repast.simphony.context.space.continuous.ContinuousSpaceFactory;
import repast.simphony.context.space.continuous.ContinuousSpaceFactoryFinder;
import repast.simphony.context.space.graph.NetworkBuilder;
import repast.simphony.context.space.grid.GridFactory;
import repast.simphony.context.space.grid.GridFactoryFinder;
import repast.simphony.dataLoader.ContextBuilder;
import repast.simphony.engine.environment.RunEnvironment;
import repast.simphony.parameter.Parameters;
import repast.simphony.random.RandomHelper;
import repast.simphony.space.continuous.ContinuousSpace;
import repast.simphony.space.continuous.NdPoint;
import repast.simphony.space.continuous.RandomCartesianAdder;
import repast.simphony.space.grid.Grid;
import repast.simphony.space.grid.GridBuilderParameters;
import repast.simphony.space.grid.SimpleGridAdder;
import repast.simphony.space.grid.WrapAroundBorders;


public class diseaseSpreadSimulatorBuilder  implements ContextBuilder<Object> {

    @Override
    public Context<Object> build(Context<Object> context) {
          context.setId("diseaseSpreadSimulator");  

          NetworkBuilder <Object> netBuilder = new NetworkBuilder<Object>("infection network", context,true);
          netBuilder.buildNetwork();

          ContinuousSpaceFactory spaceFactory = ContinuousSpaceFactoryFinder.createContinuousSpaceFactory(null);
          ContinuousSpace<Object> space = spaceFactory.createContinuousSpace("space", context, new RandomCartesianAdder<Object>(), 
                                new repast.simphony.space.continuous.WrapAroundBorders(), 50 , 50);

          GridFactory gridFactory = GridFactoryFinder.createGridFactory(null);
          Grid<Object> grid = gridFactory.createGrid("grid", context, 
                                                     new GridBuilderParameters<Object>(new WrapAroundBorders(),new SimpleGridAdder<Object>(), true, 50, 50));



          Parameters params = RunEnvironment.getInstance().getParameters();
          int infectedCount = (Integer)params.getValue("infected_count");

          for(int i = 0; i < infectedCount; i++) {
              context.add(new Infected(space, grid));
          }

          int healthyCount = (Integer)params.getValue("healthy_count");

          for(int i=0; i < healthyCount; i++ ) {
              context.add(new Healthy(space, grid));
          }

          int hospitalCount = (Integer)params.getValue("hospital_count");
          for(int i=0; i < hospitalCount; i++ ) {
              context.add(new Hospital(space, grid));
          }

            for (Object obj : context) {
                NdPoint pt = space.getLocation(obj);
                grid.moveTo(obj, (int) pt.getX(), (int) pt.getY());
            }

            if (RunEnvironment.getInstance().isBatch()) {
                RunEnvironment.getInstance().endAt(20);
            }

          return context;





    }

}

  • 2
    I tried your project. It looks like the data loader is never set up. If you use the wizard in the GUI to add your context builder, then it works. Page 19 of the Java Getting Started Guide describes how to do this. – Nick Collier May 14 '20 at 19:31

1 Answers1

2

Does the id in your context.xml match that set in your ContextBuilder? For example, in jzombies

context.xml

<context id="jzombies"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://repast.org/scenario/context"
    class="jzombies.JZombiesContext">
    <projection type="continuous space" id="space"></projection>
    <projection type="grid" id="grid"></projection>
    <projection type="network" id="infection network"></projection>
</context>

JZombiesBuilder.java

public class JZombiesBuilder implements ContextBuilder<Object> {
  @Override
  public Context build(Context<Object> context) {
    context.setId("jzombies");
    ...

Note that the id is case sensitive as well. Its not much help to you at the moment but the development version of Simphony and thus the next release has a more comprehensive error message for this problem now.

Nick

Nick Collier
  • 1,786
  • 9
  • 10