**public void testCompareTo() {
System.out.println("compareTo");
Patient p = null;
Patient instance = null;
int expResult = 0;
int result = instance.compareTo(p);
assertEquals(expResult, result);
}**
有没有办法在空对象中调用方法,或者我必须在这里执行异常吗?在空对象JAVA中调用非静态方法?
其实任何引用,语义这是不正确的。你的意思是调用空对象上的任何方法总是抛出一个NullPointerException – ControlAltDel 2014-09-29 17:30:38
有没有办法为变量“result”设置一个值?如果我不能调用compareTo并且不改变和'p '和'instance' – user3133474 2014-09-29 17:35:31
@TJCrowder这很有趣。我从来没有试过通过空对象引用调用一个静态方法。然后,我试着不通过对象引用来调用静态方法,所以我从来没有尝试过它并不是一个惊喜。 – Eran 2014-09-29 17:35:36