方法上的奇怪java递归nullpointerexception
您好,我正在编写一个检查引擎,并为玩家类定义一个canEatPoint1,Point2方法,该方法通过检查棋子和着陆路径是否在船上,根据玩家是否可以在指定点吃棋子,返回true或false,如果两个棋子是否属于同一玩家,同时考虑棋子的国王/超级地位 不管怎么说,它在没有递归的情况下工作得非常愉快,但是没有递归的情况下,方法必须得到片段的点,而不是落地片段,如果我管理递归,这是可以的。但我希望这个方法也可以考虑它是否得到了着陆点而不是块位置。当我运行代码时,它修复了位置并再次调用自己,但是没有问题。我在上得到NullPointerException caneat=Checker2.isAlive?真:假;因为Checker2是空的。我的pointToChecker方法在这家伙身上返回null,所以Checker2为null。是什么导致了这种情况呢?我对递归还是个新手,老实说,甚至是面向对象编程 这是我的代码和控制台错误日志方法上的奇怪java递归nullpointerexception,java,recursion,Java,Recursion,您好,我正在编写一个检查引擎,并为玩家类定义一个canEatPoint1,Point2方法,该方法通过检查棋子和着陆路径是否在船上,根据玩家是否可以在指定点吃棋子,返回true或false,如果两个棋子是否属于同一玩家,同时考虑棋子的国王/超级地位 不管怎么说,它在没有递归的情况下工作得非常愉快,但是没有递归的情况下,方法必须得到片段的点,而不是落地片段,如果我管理递归,这是可以的。但我希望这个方法也可以考虑它是否得到了着陆点而不是块位置。当我运行代码时,它修复了位置并再次调用自己,但是没有问题
public boolean canEat(Point Piece, Point target)
{
//INITIALIZING
//POINT1 & CHECKER1-> piece which eats
//POINT2 & CHECKER2-> piece to eat
//POINT3 & CHECKER3-> landing path
if (samePoint(Piece, target))
return false;
boolean caneat = true;
boolean king1 = false;
Point Point1,Point2,Point3;
Checker Checker1,Checker2,Checker3;
int ownerID, victimID,axis;
axis = getDirection(Piece, target);
//CHECK IF PLAYER POINTED THE SQUARE AHEAD TARGET PIECE OR THE PIECE TO EAT
int distance = getStep(Piece, target);
if(distance == 2)
{
boolean resolution;
System.err.println("Recoordinated target");
Point fixed = getBackwards(target, axis);
System.err.println("Fixed is: " + Piece + " " + fixed);
resolution = canEat(Piece, fixed);
return resolution;
}
Point1 = Piece;
Point2 = target;
Point3 = findMovePosition(target, axis);
if(!inBoard(Point1) || !inBoard(Point2) || !inBoard(Point3))
{
System.err.println("Error: Invalid Move --> Thinking outside the box");
return false;
}
Checker1 = PointToPiece(Point1);
Checker2 = PointToPiece(Point2);
Checker3 = PointToPiece(Point3);
//CHECK IF PIECES ARE ALIVE
caneat = Checker1.isAlive() ? true : false;
caneat = Checker2.isAlive() ? true : false;
if(!Checker2.isAlive())
{
return false;
}
//THERE SHOULD BE TARGET AND VICTIM PIECES AND AN EMPTY LANDING POINT
if(!isOccupied(Piece))
{
System.err.println("#OccupyChecker invalid piece to move");
return false;
}
if(!isOccupiedbyEnemy(target))
{
System.err.println("#OccupyChecker invalid piece to eat");
return false;
}
if(!(Checker3 == null))
{
System.err.println("#OccupyChecker landing piece error");
return false;
}
ownerID = Checker1.getOwner().getID();
victimID = Checker2.getOwner().getID();
if(ownerID == victimID)
{
System.err.println("owner ID: " + ownerID + " victimID: " + victimID);
System.err.println("#OCCUPY identity");
return false;
}
if(ownerID == 1 && king1)
{
if(axis == 3 || axis == 4)
System.err.println("#OccupyPoint wrong axis");
caneat = false;
}
if(ownerID == 2 && king1)
{
if(axis == 1 || axis == 2)
System.err.println("#OccupyPoint wrong axis");
caneat = false;
}
return caneat;
}
Checker PointToPiece(Point piece)
{
Checker[] allPieces = allPieces();
Checker found = null;
for(Checker k : allPieces)
{
if(samePoint(k.getPosition(), piece))
found = k;
}
return found;
}
pointtopice是一个假设填充这些变量的函数,因此发布它将非常方便。。此外,完整的异常输出也很有用。Checker2仅通过调用pointToPiecePoint2来设置,因此这一定是失败的原因。您尚未向我们展示该函数,但如果您找出可能导致它返回null的原因。。。顺便说一句,惯例是变量名应该以小写字母开头;只有类型应该以大写开头。抱歉,我没有意识到这是必需的,我马上发布了。我刚刚添加了这个方法。我在名为Player的单个类中实现了大多数方法。我认为把所有代码都放在这里,给阅读它的人带来不必要的疲劳是错误的。向周围的新手道歉here@photonist,我建议您调试pointtopice,看看它为什么不输入found=k,因为您希望它。。