0

I'm working on a program using eclipse that generates objects for runners in a 100 m race. each runner has a lane, name and three separate times for completing the race. However, when I try to generate the times for each object, I get java.lang.NullpointerException.

Here is the method for generating the times.

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;
}

and then the code in which the method is called.

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);
}

I'd try to fix it through debugger, but every time i try to step-into the for loop, i get InvocationTargetException,(Throwable line: not available

initialization of times, roll and rolled:

    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;

and of performance:

    public class PerformanceTest {
Performance performance;

@Before
public void setup() {
    performance = new Performance(1, "", new double[]{9.2, 9.4});
}
  • Where do you declare and initialize `times`, `roll` and `rolled`? – Paul Boddington May 03 '15 at 01:53
  • Also, where do you declare and initialize `performance`? – Rumal May 03 '15 at 01:54
  • Please post something that will compile. Things that I notice that are missing. 1) What is `times` within the `getTImes` method? 2) What is `rolled` within the `getTimes` method? 3) What is `roll` within the `getTimes` method? 4) What is `performance` within the `testGetTimes` method? I presume this is a part of a larger class, but without seeing that class, it's difficult to determine what's happening. – Michael Price May 03 '15 at 01:56
  • just looked at my initialization, i hadnt set an index value for times, so that was my nullpointerexception. can't believe i missed that... however now i'm getting an AssertionError: Expected 9.2 but was 44.77 – malignantWizard May 03 '15 at 01:57
  • I don't think there's enough information just from what you've posted to prove that there is or was a NullPointerException. Could you highlight the exact line that this occurred on? – Makoto May 03 '15 at 02:09
  • Since the information you provided is incomplete, we can't help you. I have therefore marked this as a duplicate of a Q&A with lots of advice on how to solve NPE problems for yourself. – Stephen C May 03 '15 at 02:24

1 Answers1

-1

It looks like one of your objects is uninitialized. On the other hand, initializing it to null can cause the same problem. As of Java 8, I would suggest using an Optional to contain any values that you are not immediately certain of, or are not immediately aware of, and put their processing in an Optional.ifPresent(Consumable) statement. It minimizes the possibility of a NullPointerException, and invites functional programming methods, which will seriously slim down the amount of time it takes to get the job done.

As far as your assertion error goes, I'm sure you already know that this is only caused by an assert statement. Looks like times[0] is substantially larger than you were expecting. It appears, from what we can see, that you're setting it to 1.0 + random{0.0...1.0} * 100.0; I don't know what the precondition that leads you to expect 9.4 is, but this could easily hit the forties.

Michael Macha
  • 1,729
  • 1
  • 16
  • 25
  • 1
    Slow down here. There's nothing to suggest in the code previously posted or currently up that any of the objects were null. We don't have enough information to conclude that an object is uninitialized (although that is very much likely the case). – Makoto May 03 '15 at 02:11
  • I have to disagree with you, Makoto. NullPointerExceptions are only thrown when null is accessed as if it is an actual value; and since he isn't throwing it, it can be safely assumed (until feedback) that something hasn't been initialized. – Michael Macha May 03 '15 at 02:25
  • Sorry guy's, i'm quite new at java and also this website! i fixed the nullpointerexception and now have figured out the source of the assertion error. will work through it on my own just for better practice. Thanks for all your help! sorry for duplicating the question! – malignantWizard May 03 '15 at 02:26