public Boolean performAction(AppleCollectorAgent agent, data.ActionType action)
{
if(agent != null && action != null)
{
actions.put(agent, action);
}
else
{
System.out.println("GRID: "+agent+" performs "+action+" TID: "+Thread.currentThread().getId()+". Time: "+ new Date().getTime());
System.out.println("Either agent or action was null: "+agent+" - "+action);
}
}
由调用
ActionType ac = ActionType.ApplePickup;
System.out.println("AGENT: "+myAgent+" going to perform "+ac+" TID: "+Thread.currentThread().getId()+". Time: "+ new Date().getTime());
success = GridWorld.get().performAction((AppleCollectorAgent)myAgent, ac);
这适用于enum Action的其他值,但是对于值Action.PickupApple,actions.put抛出了一个NullPointerException。当我放置一些println来显示参数的值时,它变得更加奇怪。在调用之前,ac被打印为PickApple,而在performAction中,action被打印为空:
GRID: Agents.GreedyAgent@1a42792 performs null TID: 29. Time: 1296317211796
Either agent or action was null: Agents.GreedyAgent@1a42792 - null
AGENT: Agents.GreedyAgent@1a42792 going to perform ApplePickup TID: 29. Time: 1296317211796
那么,如何在performAction中将操作变为空呢?
关于背景的一些解释:对于一门关于多智能体系统的课程,我必须模拟一个网格世界,在这个网格世界中,智能体可以四处走动并摘苹果。在每个模拟步骤中,每个智能体都可以执行一个动作。操作类型存储在枚举data.Action中。actions是一个ConcurrentHashMap,每个agent在其中存储他想要执行的操作。当所有代理都完成这些操作后,网格世界将处理所有这些操作,并返回一个布尔值,指示操作的成功。每个代理都有自己的线程,网格世界也是如此。再往下看performAction(),有一些同步机制。我最初认为是多线程出了问题,但我想我可以放心地说这不是问题所在。action为空,这就是问题所在。
发布于 2011-01-30 00:11:08
从注释中推断,我会说performAction
被调用了两次。尤其是当它看起来“有时”有效的时候。一次操作为空,一次操作为PickApple。
如果是这种情况,您可以使用Thread.currentThread().getStackTrace()
从调用该方法的位置开始计算。
https://stackoverflow.com/questions/4837382
复制相似问题