1

I'm testing two activities: the first one completes its tests and finishes, but the first test in the second one crashes with the following error:

Test failed to run to completion. Reason: 'Instrumentation run failed due to 'java.lang.NullPointerException''. Check device logcat for details

I figured the line activity = getActivity(); in the test class gives a null and thus crashes the test. Here's the relevant part of the test class. Why does this happen, and how do I fix it?

public ResultActivityTest() {
    super(ResultActivity.class);
}

@Override
protected void setUp() throws Exception {
    super.setUp();

    activity = getActivity();
    displayoperands = (TextView) activity.findViewById(R.id.result_textview);
    resultdisplay = (TextView) activity.findViewById(R.id.textView2);
    canvas = (SurfaceView) activity.findViewById(R.id.surfaceview);
    layoutRoot = (RelativeLayout) activity.findViewById(R.id.rellay);
    back = (Button) activity.findViewById(R.id.back_button);

    activity.setImag1(2.0);
    activity.setImag2(2.0);
    activity.setReal1(2.0);
    activity.setReal2(2.0);
    activity.setOperation("A");
}
Sincress
  • 107
  • 1
  • 14
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Kenster Dec 10 '14 at 20:35

1 Answers1

2

I figured it out after a bit of tampering with the activity and some trial and error. The problem was, the activity's onCreate demanded a bundle with variables from the calling activity. This being null, kept crashing the activity that the test tried to start without passing any variables. The solution is to initialise the required variables with a (dummy/default) value that works with the activity.

Sincress
  • 107
  • 1
  • 14