1

I am a new Repast user, could you please help me fixing this error: I have a run time error as you can see below. When i commenting the addEdge command, the program runs well net. <<< addEdge (this , zombie ); >>>

Please note that my code is exactly the same like the tutorial.

The error i have as follows:

FATAL [Thread-7] 14:34:36,287 repast.simphony.ui.GUIScheduleRunner - RunTimeException when running the schedule
Current tick (14.0)
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
 at repast.simphony.engine.schedule.DynamicTargetAction.execute(DynamicTargetAction.java:72)
 at repast.simphony.engine.controller.ScheduledMethodControllerAction$ScheduleMethodAllAction.execute(ScheduledMethodControllerAction.java:333)
 at repast.simphony.engine.schedule.DefaultAction.execute(DefaultAction.java:38)
 at repast.simphony.engine.schedule.ScheduleGroup.executeList(ScheduleGroup.java:205)
 at repast.simphony.engine.schedule.ScheduleGroup.execute(ScheduleGroup.java:231)
 at repast.simphony.engine.schedule.Schedule.execute(Schedule.java:352)
 at repast.simphony.ui.GUIScheduleRunner$ScheduleLoopRunnable.run(GUIScheduleRunner.java:52)
 at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
 at jzombies.Zombie$$FastClassByCGLIB$$6141f31.invoke(<generated>)
 at net.sf.cglib.reflect.FastMethod.invoke(FastMethod.java:53)
 at repast.simphony.engine.schedule.DynamicTargetAction.execute(DynamicTargetAction.java:69)
 ... 7 more
Caused by: java.lang.NullPointerException
 at jzombies.Zombie.infect(Zombie.java:106)
 at jzombies.Zombie.step(Zombie.java:60)
 ... 10 more

The following is my classes:

Zombies:

package jzombies;

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;

/**
 * @author gamal91
 *
 */
public class Zombie {

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

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

 // (Annotation) To iterate each time step.
 @ScheduledMethod  (start = 1 ,interval = 1)
 public void step() {
  // Get the grid location of this Zombie.
  GridPoint pt = grid.getLocation(this);

  GridCellNgh<Human> nghCreator = new GridCellNgh<Human>(grid, pt, Human.class, 1, 1);

  List<GridCell<Human>> gridCells = nghCreator.getNeighborhood(true);

  SimUtilities.shuffle(gridCells, RandomHelper.getUniform());

  GridPoint pointWithMostHumans = null;
  int maxCount = -1;

  for (GridCell<Human> cell : gridCells) {
   if (cell.size() > maxCount) {
    pointWithMostHumans = cell.getPoint();
    maxCount = cell.size();
   }
  }
  moveTowards(pointWithMostHumans);
  infect();
 }

 public void moveTowards(GridPoint pt) {

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

   // You may delete the zero and see what will happen
   space.moveByVector(this, 1, angle, 0);

   myPoint = space.getLocation(this);
   grid.moveTo(this, (int) myPoint.getX(), (int) myPoint.getY());

   moved = true;
  }
 }

 @SuppressWarnings("unchecked")
 public void infect() {
  GridPoint pt = grid.getLocation(this);
  List<Object> humans = new ArrayList<Object>();
  for (Object obj : grid.getObjectsAt(pt.getX(), pt.getY())) {
   if (obj instanceof Human) {
    humans.add(obj);
   }
  }

  if (humans.size() > 0) {
   int index = RandomHelper.nextIntFromTo(0, humans.size() - 1);
   Object obj = humans.get(index);
   NdPoint spacePt = space.getLocation(obj);
   Context<Object> context = ContextUtils.getContext(obj);
   context.remove(obj);
   Zombie zombie = new Zombie(space, grid);
   context.add(zombie);
   
   space.moveTo(zombie, spacePt.getX(), spacePt.getY());
   grid.moveTo(zombie, pt.getX(), pt.getY());
   
   Network<Object> net = (Network<Object>) context.getProjection("infection netwrok");
   net.addEdge(this, zombie);
  }
 }
}

Humans Class

package jzombies;

import java.util.List;

import repast.simphony.engine.watcher.Watch;
import repast.simphony.engine.watcher.WatcherTriggerSchedule;
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;

/**
 * @author gamal91
 *
 */
public class Human {
 private ContinuousSpace<Object> space;
 private Grid<Object> grid;
 private int energy, startingEnergy;

 public Human(ContinuousSpace<Object> space, Grid<Object> grid, int energy) {
  this.space = space;
  this.grid = grid;
  this.energy = startingEnergy = energy;
 }

 
 @Watch (watcheeClassName = "jzombies.Zombie" , 
   watcheeFieldNames = "moved",
   query = "within_moore 1",
   whenToTrigger = WatcherTriggerSchedule.IMMEDIATE)
   
   
 public void run() {
  GridPoint pt = grid.getLocation(this);

  GridCellNgh<Zombie> nghCreator = new GridCellNgh<Zombie>(grid, pt, Zombie.class, 1, 1);
  List<GridCell<Zombie>> gridCells = nghCreator.getNeighborhood(true);

  SimUtilities.shuffle(gridCells, RandomHelper.getUniform());

  GridPoint pointWithLeastZombies = null;
  int minCount = Integer.MAX_VALUE;

  for (GridCell<Zombie> cell : gridCells) {
   if (cell.size() < minCount) {
    pointWithLeastZombies = cell.getPoint();
    minCount = cell.size();
   }
  }

  if (energy > 0) {
   moveTowards(pointWithLeastZombies);
  } else {
   energy = startingEnergy;
  }
 }

 public void moveTowards(GridPoint pt) {
  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);
   
   // Moves it two units along the calculated angel.
   space.moveByVector(this, 2, angle, 0);

   myPoint = space.getLocation(this);

   grid.moveTo(this, (int) myPoint.getX(), (int) myPoint.getY());
   energy--;

  }
 }
}

jZobies Class :

package jzombies;

// import repast.simphony.engine.environment.RunEnvironment;
// import repast.simphony.context.DefaultContext;

import repast.simphony.context.Context;
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.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 JZobieBuilder implements ContextBuilder<Object> {

 @Override
 public Context build(Context<Object> context) {

  // Should match the project name
  context.setId("jzombies");

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

  ContinuousSpaceFactory spaceFactory = ContinuousSpaceFactoryFinder.createContinuousSpaceFactory(null);
  ContinuousSpace<Object> space = spaceFactory.createContinuousSpace("space", context,
    // Anything is added to this space will be randomly located.
    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>(
    // More than one obejct are allowed to occupy a grid point.
    new WrapAroundBorders(), new SimpleGridAdder<Object>(), true, 50, 50));

  int zombieCount = 20;
  for (int i = 0; i < zombieCount; i++) {
   context.add(new Zombie(space, grid));
  }

  int humanCount = 100;
  for (int i = 0; i < humanCount; i++) {
   int energy = RandomHelper.nextIntFromTo(4, 10);
   context.add(new Human(space, grid, energy));
  }

  for (Object obj : context) {
   NdPoint pt = space.getLocation(obj);
   grid.moveTo(obj, (int) pt.getX(), (int) pt.getY());
  }
  return context;
 }
}
Gamal Eldeeb
  • 49
  • 1
  • 5

1 Answers1

4

It looks like you've misspelled "infection network" in the context.getProjection call.

J. Ozik
  • 1,083
  • 5
  • 6