我正在研究一个使用eclipse的程序,它可以在100米比赛中为跑步者生成对象。 每个跑步者都有一个车道,名称和三个单独的时间来完成比赛。 但是,当我尝试为每个对象生成时间时,我得到java.lang.NullpointerException。
这是生成时间的方法。
public double[] getTimes() {
for (int i = 0; i <= (times.length - 1); i++) {
rolled = 1.0 + roll.nextDouble() * 100.0;
// set rolled to a new random number between 1 and 100
times[i] = rolled;
// set index i of the array times to be the nearest
// double to roll's value.
}
return times;
}
然后调用该方法的代码。
public void testGetTimes() {
double[] times = performance.getTimes();
assertEquals(2, times.length);
assertEquals(9.2, times[0], 0.01);
assertEquals(9.4, times[1], 0.01);
}
我试图通过调试器修复它,但每次我尝试进入for循环时,我都会得到InvocationTargetException,(Throwable line:not available
初始化时间,滚动和滚动:
public class Performance {
private int lane;
private String name;
double[] times = new double[3];
int rolling;
Random roll = new Random();
double rolled;
double average;
double best;
和表现:
public class PerformanceTest {
Performance performance;
@Before
public void setup() {
performance = new Performance(1, "", new double[]{9.2, 9.4});
}