LoginSignup
1
1

More than 5 years have passed since last update.

AndroidのTestCaseで、SQLのgetReadableDataBase()でNullPointerExceptionが発生する

Posted at

AndroidのTestCaseで、SQLのgetReadableDataBase()でNullExceptionが発生した。

調べていく中で、contextの取得がうまくいっていないっぽかった。
ちなみに、使っていたのは ActivityUnitTestCase で、以下のように context を取得していた。

 Context con = this.getActivity().getApplicationContext()

調べると、この記事に到達。

Android の jUnit テストで Context が欲しい時

下記のように取得するように書いてある。

 Context con = this.getInstrumentation().getTargetContext().getApplicationContext();

このコードを使って context を取得したら、NullPointerException は出なくなった。

やったね!

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1