2

Hi am getting a RunTimeException java.lang.NullPointerException on context.remove(this) in the recover method. When running this simulation it runs correctly initially but then stops after a short period due to this issue. Any help is appreciated

@ScheduledMethod(start = 1, interval = 1)
public void step() {
    
    this.daysInfected++;
    boolean isRecovered = checkRecover();
    boolean isDead = checkDead();
        
        if(!isDead && !isRecovered) {
            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 ptNew = cell.getPoint();
            moveTowards(ptNew);
            
            infect();
        }
    if(daysInfected >= maxDays) {
        recover();
    }
}

public boolean checkRecover() {
    double r = Math.random();
    if(r <= chanceRecovering) {
        recover();
        return true;
    }
    return false;
}

public void recover() {
    GridPoint pt = grid.getLocation(this);
    NdPoint spacePt = space.getLocation(this);
    Context<Object>  context = ContextUtils.getContext(this);
    context.remove(this);
    Recovered recovered= new Recovered(space, grid);
    context.add(recovered);
    space.moveTo(recovered, spacePt.getX(), spacePt.getY());
    grid.moveTo(recovered, pt.getX(), pt.getY());
}
ScottB3008
  • 31
  • 3

1 Answers1

3

The most likely cause is that the agent has not been previosly added to the context and ContextUtils.getContext(this) is returning null.

Eric Tatara
  • 715
  • 3
  • 12