# android.app.NotificationManager: java.lang.NullPointerException

**URL:** <https://discuss.kotlinlang.org/t/android-app-notificationmanager-java-lang-nullpointerexception/21114>\
**Category:** Android\
**Created:** [March 7, 2021, 10:06am UTC](https://discuss.kotlinlang.org/t/android-app-notificationmanager-java-lang-nullpointerexception/21114 "2021-03-07T10:06:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![djlj](https://avatars.discourse-cdn.com/v4/letter/d/49beb7/32.png) [@djlj](https://discuss.kotlinlang.org/u/djlj)\
**Post date:** [March 7, 2021, 10:06am UTC](https://discuss.kotlinlang.org/t/android-app-notificationmanager-java-lang-nullpointerexception/21114/1 "2021-03-07T10:06:40Z")

</div>

This code:  
`notificationManager = getSystemService(requireContext(), MyClass::class.java) as NotificationManager`

Ends with this error:  
`java.lang.NullPointerException: null cannot be cast to non-null type android.app.NotificationManager`

I searched at the internet hours and hours but without success. What variable/class is null? What i must to do?

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [March 7, 2021, 11:47am UTC](https://discuss.kotlinlang.org/t/android-app-notificationmanager-java-lang-nullpointerexception/21114/2 "2021-03-07T11:47:38Z")

</div>

Basically, the return value of `getSystemService` in this case is null. This is because you’re meant to pass in the class of the service that you want, not that `MyClass`, and so your code should become: `getSystemService(requireContext(), NotificationManager::class.java) as NotificationManager`

---

<div class="post-metadata">

**Author:** ![djlj](https://avatars.discourse-cdn.com/v4/letter/d/49beb7/32.png) [@djlj](https://discuss.kotlinlang.org/u/djlj)\
**Post date:** [March 7, 2021, 8:53pm UTC](https://discuss.kotlinlang.org/t/android-app-notificationmanager-java-lang-nullpointerexception/21114/3 "2021-03-07T20:53:05Z")

</div>

I understand. That makes sense. So what must be return of called class `NotificationManager`?

---

<div class="post-metadata">

**Author:** ![djlj](https://avatars.discourse-cdn.com/v4/letter/d/49beb7/32.png) [@djlj](https://discuss.kotlinlang.org/u/djlj)\
**Post date:** [March 8, 2021, 5:03pm UTC](https://discuss.kotlinlang.org/t/android-app-notificationmanager-java-lang-nullpointerexception/21114/4 "2021-03-08T17:03:59Z")

</div>

There’s `getSystemService(Context.NOTIFICATION_SERVICE)` at to many example codes on the internet. But Android studio shows error:

> Type mismatch.  
> Required: Context  
> Found: String

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [March 8, 2021, 5:05pm UTC](https://discuss.kotlinlang.org/t/android-app-notificationmanager-java-lang-nullpointerexception/21114/5 "2021-03-08T17:05:27Z")

</div>

Then you need to do `getSystemService(requireContext(), Context.NOTIFICATION_SERVICE)`

---

<div class="post-metadata">

**Author:** ![djlj](https://avatars.discourse-cdn.com/v4/letter/d/49beb7/32.png) [@djlj](https://discuss.kotlinlang.org/u/djlj)\
**Post date:** [March 8, 2021, 7:54pm UTC](https://discuss.kotlinlang.org/t/android-app-notificationmanager-java-lang-nullpointerexception/21114/6 "2021-03-08T19:54:48Z")

</div>

Yes! Thank you! I got it! After hours and hours.

> val notificationManager = activity?.getSystemService(Context.NOTIFICATION\_SERVICE) as NotificationManager  
> …  
> notificationManager.notify(NOTIFICATION\_ID, builder.build())

It works! 🙃
