Reputation: 839
I have the following line of code:
final int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
For which I am receiving the following warning on the "getStreamMaxVolume" keyword in Android Studio:
"Method invocation 'getStreamMaxVolume' may produce 'java.lang.NullPointerException'"
Would anyone know how to resolve this warning?
Upvotes: 0
Views: 239
Reputation: 3744
You get the warning message because the method, getStreamMaxVolume()
sometimes throws the null pointer exception. So, in order to make the warning go away, you have to handle the Exception like this.
try
{
final int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
}
catch(java.lang.NullPointerException exception)
{
//how you want to handle the exception
}
Upvotes: 2
Reputation: 8670
if(audioManager!=null){
final int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
}
keep a check around the call for null
check
Upvotes: 1