我正在使用MASON库运行一个简单的基于代理的模型。
按照规范,我想通过双击这种代理来访问代理的检查员。
但是,当我这样做时,我会得到以下控制台错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at sim.display.Display2D.createInspectors(Display2D.java:1737)
at sim.display.Display2D$8.mouseClicked(Display2D.java:1392)我去了Display2D.java:1737,发现:
public void createInspectors( final Rectangle2D.Double rect, final GUIState simulation )
{
Bag inspectors = new Bag();
Bag names = new Bag();
Bag[] hitObjects = objectsHitBy(rect);
for(int x=0;x<hitObjects.length;x++)
{
FieldPortrayal2DHolder p = (FieldPortrayal2DHolder)(portrayals.get(x));
for( int i = 0 ; i < hitObjects[x].numObjs ; i++ )
{
LocationWrapper wrapper = (LocationWrapper) (hitObjects[x].objs[i]);
inspectors.add(p.portrayal.getInspector(wrapper,simulation));
names.add(p.portrayal.getName(wrapper));
}
}
simulation.controller.setInspectors(inspectors,names); //1737
}然而,这是一个库文件,所以我不熟悉它。
有什么建议吗?
图书馆:参阅gmu.edu/~eclab/projects/mason/
更新:
好吧,这很有趣..。
我对检查人员和名称的toString方法做了一个回音,返回:
insepectors sim.util.Bag@1b2202a names sim.util.Bag@16b334d好的,它们是袋子,一种收藏品。是时候弄清楚他们的尺寸了。
insepectors 1 names 1很好,它们不是空的。
让我们按照错误堆栈
下一段:
at sim.display.Display2D$8.mouseClicked(Display2D.java:1392)
// add mouse listener for the inspectors
insideDisplay.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if (handleMouseEvent(e)) { repaint(); return; }
else
{
// we only care about mouse button 1. Perhaps in the future we may eliminate some key modifiers as well
int modifiers = e.getModifiers();
if ((modifiers & e.BUTTON1_MASK) == e.BUTTON1_MASK)
{
final Point point = e.getPoint();
if( e.getClickCount() == 2 )
1392-> createInspectors( new Rectangle2D.Double( point.x, point.y, 1, 1 ),
Display2D.this.simulation );
if (e.getClickCount() == 1 || e.getClickCount() == 2) // in both situations
performSelection( new Rectangle2D.Double( point.x, point.y, 1, 1 ));
repaint();
}
}
}好的,错误明显出现在CreateInspectors方法中:
public void createInspectors( final Rectangle2D.Double rect, final GUIState simulation )
{
Bag inspectors = new Bag();
Bag names = new Bag();
Bag[] hitObjects = objectsHitBy(rect);
for(int x=0;x<hitObjects.length;x++)
{
FieldPortrayal2DHolder p = (FieldPortrayal2DHolder)(portrayals.get(x));
for( int i = 0 ; i < hitObjects[x].numObjs ; i++ )
{
LocationWrapper wrapper = (LocationWrapper) (hitObjects[x].objs[i]);
inspectors.add(p.portrayal.getInspector(wrapper,simulation));
names.add(p.portrayal.getName(wrapper));
}
}
System.out.println("insepectors " + inspectors.size() + " names " + names.size());
simulation.controller.setInspectors(inspectors,names);
}我做的第一件事:
System.out.println(rect.getCenterX() + " " + rect.getCenterY());给我646.5 659.5。作为坐标似乎是有意义的。
接下来我想看看hitObjects:
System.out.println(hitObjects.length);返回2,所以我在坐标处有两个探员。
我想NPE就在这里:
for(int x=0;x<hitObjects.length;x++)
{
FieldPortrayal2DHolder p = (FieldPortrayal2DHolder)(portrayals.get(x));
for( int i = 0 ; i < hitObjects[x].numObjs ; i++ )
{
LocationWrapper wrapper = (LocationWrapper) (hitObjects[x].objs[i]);
inspectors.add(p.portrayal.getInspector(wrapper,simulation));
names.add(p.portrayal.getName(wrapper));
}
}外循环看起来很好,但我对内部循环有问题。建议?
发布于 2015-03-31 23:31:04
实际上,我将编写如何通过给出的库代码进行推理,因为你听起来很有能力。
你找到密码了。这是个很好的开始。你还找到了上面的线路:
simulation.controller.setInspectors(inspectors,names); //1737考虑到您对NPEs的了解,您可以推断simulation或controller是null。
你知道哪一个吗?您能对您可能设置错误或传递错误的代码进行推理吗?您能否在代码中放置调试器断点或简单的println语句以确定哪个语句?
如果没有使用IDE (很可能是这样),那么在您刚刚打开的源中,在第1737行之前放置一个断点。到达该行时,使用调试器检查变量simulation,然后检查simulation.controller,以查看哪个是null。确切地说,如何做到这一点将取决于您的IDE,但是找到使用XKCD的技术支持备忘单应该不难。
那你就可能知道原因了。如果没有,请继续阅读源代码,例如,查看一个级别到第1737行。
https://stackoverflow.com/questions/29380191
复制相似问题