Fix Method invocation ‘vibrate may produce java.lang.NullPointerException’

Here is how you can Fix the “Method invocation ‘vibrate’ may produce ‘java.lang.NullPointerException” Error in Android Studio Java

This error is common and can be easily fixed with the following code, it works by encapsulating the java vibrate action code in the if statement

Here is the java code to fix the error

Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
if(vibrator != null) {

For this code to work, you need add vibration permission in the android manifest

<uses-permission android:name="android.permission.VIBRATE"/>

Leave a Reply

Your email address will not be published.