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) { vibrator.vibrate(100); }
For this code to work, you need add vibration permission in the android manifest
<uses-permission android:name="android.permission.VIBRATE"/>