Tymek T. Tymek T. - 4 months ago 11
Android Question

NullPointerException while testing app

I've got a problem while testing my app, class "DateToday" exactly, here's part of that class:

public DateToday(){
monthName();
dayName();
dayNumber();
yearNumber();
}

public int yearNumber(){
year = c.get(Calendar.YEAR);
return year;
}


And here's my test class:

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class DateTodayTest {

private DateToday mDateToday;

@Test
public void checkYear() {
assertEquals(mDateToday.yearNumber(), 2016);
}


Why I am still having
NullPointerException
at
checkYear()
method and the test is failed? It may be a stupid issue but I am blind with that...

Answer

It seems mDateToday is not initialized, you should use the constructor:

private DateToday mDateToday =  new DateToday ();