JF0001
JF0001

Reputation: 839

NullPointerException on getStreamMaxVolume

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

Answers (2)

Nipun Thennakoon
Nipun Thennakoon

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

Anmol
Anmol

Reputation: 8670

if(audioManager!=null){
   final int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
}

keep a check around the call for null check

Upvotes: 1

Related Questions