我的代码可以在这里找到:http://pastebin.com/tmzGgHzi
但是,当我第二次单击"step“时,它在第67行抛出了一个NullPointerException。这是因为getAdjacentLocation()方法引用了一个空对象,但是我搞不懂,我的AP老师也搞不懂。
任何帮助都是非常感谢的!谢谢!
发布于 2014-02-01 08:37:13
getLocation()方法返回null。
Location loc = this.getLocation();
//Loc is now null, so the following call will result in a null pointer exception
Location newLoc = loc.getAdjacentLocation(getDirection());这是因为它正在将自己从网格中移除/它正在吞噬自己
public ArrayList<Actor> getActors(){
ArrayList<Actor> list = new ArrayList<Actor>();
Grid<Actor> gr = getGrid();
Location loc = getLocation();
atLocation = atRandomLocation();
if(!atLocation){
setDirection(loc.getDirectionToward(randomLocation));
//THIS RETURNS THE CRITTER'S CURRENT LOCATION!
Actor a = gr.get(getLocation());
if(a != null)
//This critter is not going to be null, so it adds it to the list
list.add(a);
}else
getRandomLocation();
//This list contains all actors to be eaten
return list;
}您的方法所做的是检查它是否在该位置,将其自身添加到列表中,然后吃掉它自己!
public void processActors(ArrayList<Actor> actors){
for(Actor a : actors)
//This list only contains the critter
a.removeSelfFromGrid();
}您需要做的是找到它应该移动到的下一个位置,吃掉该空间中的任何角色,然后移动。
附言:显然,如果Critter是唯一的演员,它会默认地吃自己。
https://stackoverflow.com/questions/16111289
复制相似问题