NullPointerException – getInstrumentation().getContext()

Posted by in Software

Here’s a beginner mistake… The following code:

Throws an exception:

Attempt to invoke virtual method ‘android.content.Context android.app.Instrumentation.getContext()’ on a null object reference

It took me some time to find out why: getInstrumentation() returns null in the constructor :)

The correct way is to use setUp():

Then, you can call super in classes extending this one.

HTH,


A little experiment: If you find this post and ad below useful, please check the ad out :-)