0

I have this method in one of my classes:

...
protected Timer timer;
...
public void pauseResume()
{
    if (this.timer.isRunning()) {
        this.timer.stop();
    }
    else
        this.timer.start();
}

And then I am trying to test if this works using JUnit:

@Test
public void testPauseMethod()
{
    NewClass class1 = new NewClass();
    class1.pauseResume();
    assertTrue(class1.timer.isRunning());
}

The JUnitTest fails at class1.pauseResume(); and gives me a NullPointerException. Anyone knows what the problem is?

dic19
  • 17,821
  • 6
  • 40
  • 69
Fjondor
  • 39
  • 7
  • Is `timer` initialized? – Marv Dec 17 '14 at 15:33
  • 1
    Is it `timer` initialized within `NewClass` constructor? If not then you have the cause of the NPE. – dic19 Dec 17 '14 at 15:33
  • 1
    http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it/24100776#24100776 – assylias Dec 17 '14 at 15:35
  • Oh, it was not. Thanks, is that the normal cause of NPE? – Fjondor Dec 17 '14 at 15:36
  • 2
    Thats the only cause of an NPE... when attempt to access something on a null object. The reasons for the object being null vary, but its always because you have a reference to nothing. – Mark W Dec 17 '14 at 15:38

0 Answers0