Java 斗狗

Java 斗狗,java,nullpointerexception,Java,Nullpointerexception,我做这种编码已经有一段时间了。所以我想知道是否有什么地方我必须做出改变,以使我能够制作一个合适的java测试仪 public void performMove(Dog otherDog, float randomNumber) { /* Parameters for attack vs. Attack */ if(!(this.isDefending)&&!(otherDog.isDefending)){ if((randomNumber * 100) &

我做这种编码已经有一段时间了。所以我想知道是否有什么地方我必须做出改变,以使我能够制作一个合适的java测试仪

  public void performMove(Dog otherDog, float randomNumber) {


  /* Parameters for attack vs. Attack */
  if(!(this.isDefending)&&!(otherDog.isDefending)){
    if((randomNumber * 100) >= speed){
      otherDog.getHit((int)Math.round(0.25*power));
    }
    else{
      resultOfLastInteraction = "...." + this.name + "miss"; 
    }

  }
  /*Parameters for Attack vs. Defend*/
  else if(!(this.isDefending) && (otherDog.isDefending)){

    if((randomNumber * 100 >= 0) && (randomNumber * 100 <= 50)){
      otherDog.getHit((int)Math.round(0.25*power));
    }
    else if((randomNumber * 100 >= 51) && (randomNumber * 100 <= 75)){
      this.getHit((int)Math.round(0.25*power));
    }
    else if((randomNumber * 100 >= 76) && (randomNumber * 100 <= 100)){
      this.getHit((int)Math.round(0.25*power));

  }

    /*Parameters for Defend vs. Defend */
  else if((this.isDefending) && (otherDog.isDefending)){
    resultOfLastInteraction = "Stale Mate";
  }
  else{
    resultOfLastInteraction = "Select Move";
  }
  }

/* stubbed */

}
public void performMove(Dog otherDog,float random number){
/*攻击与攻击的参数*/
如果(!(this.isdefinding)和(!(otherDog.isdefinding)){
如果((随机数*100)>=速度){
otherDog.getHit((int)Math.round(0.25*power));
}
否则{
resultOfLastInteraction=“..”+this.name+“miss”;
}
}
/*攻击与防御的参数*/
else if(!(this.isdefinding)和&(otherDog.isdefinding)){

如果((randomNumber*100>=0)&&&(randomNumber*100=51)&&&(randomNumber*100=76)&(randomNumber*100我不确定
resultOfLastInteraction()
Dog
类中的实现(我假设它有一个类似的名称),但它必须返回
null


另外,为了澄清,通过“返回
NullPointerException
”,正确的术语是说抛出
NullPointerException

您的
dog2.resultOfLastInteraction()
返回NULL且未设置。如果了解
assertEquals
assertNotEquals
方法,可以提高测试的可读性。公共字符串resultOfLastInteraction(){return resultOfLastInteraction;}@Carl嗯,我认为可以安全地假设,
resultOfLastInteraction
在某个点被设置为
null
,或者它甚至根本没有被初始化。在命令的实现中,我也有以下命令:else{resultOfLastInteraction=“…”+this.name+“miss”}dog1.performMove(dog2,0.6F);此方法应该以某种方式设置ResultToFastInteraction字符串。每个dog类是否都有一个在收到攻击时调用的方法?应该在那里设置。我是否应该将迄今为止的代码通过电子邮件发送给您?因为某个地方告诉我,我把所有这些都搞糟了