Nikhil Nayak
Nikhil Nayak

Reputation: 1

Audiomanager android error null reference

I have a GPSCheck Service which produces alert sound when gps is turned off. I am getting null reference error. There's something wrong with the context. Where am I going wrong?

public class GPSCheck extends BroadcastReceiver {

public Context context;
public Ringtone ringtone;
@Override
public void onReceive(Context context, Intent intent) {
    LocationManager locationManager = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
    AlarmService obj = new AlarmService();
    if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        stopSiren();
    }
    else {
        Toast.makeText(context, "Please switch on the GPS", Toast.LENGTH_LONG).show();
        blowSiren();
    }
}

public void blowSiren() {
    AudioManager audioManager = (AudioManager) this.context.getSystemService("audio");
    audioManager.setStreamVolume(4, audioManager.getStreamMaxVolume(4), 0);
    Uri path = Uri.parse("android.resource://" + this.context.getPackageName() + "/raw/alert");
    RingtoneManager.setActualDefaultRingtoneUri(this.context, 4, path);
    this.ringtone = RingtoneManager.getRingtone(this.context, path);
    this.ringtone.setStreamType(4);
    this.ringtone.play();
}

public void stopSiren() {
    AudioManager audioManager = (AudioManager) this.context.getSystemService("audio");
    audioManager.setStreamVolume(4, audioManager.getStreamMaxVolume(4), 0);
    Uri path = Uri.parse("android.resource://" + this.context.getPackageName() + "/raw/alert");
    RingtoneManager.setActualDefaultRingtoneUri(this.context, 4, path);
    this.ringtone = RingtoneManager.getRingtone(this.context, path);
    this.ringtone.setStreamType(4);
    this.ringtone.stop();
}

}

This is the error I m geting : -

11-18 21:26:36.784 1411-1411/com.track.client E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: com.track.client, PID: 1411
                                                                    java.lang.RuntimeException: Unable to start receiver com.pasupatigroup.client.GPSCheck: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
                                                                        at android.app.ActivityThread.handleReceiver(ActivityThread.java:2789)
                                                                        at android.app.ActivityThread.access$1800(ActivityThread.java:154)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1468)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:234)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5526)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
                                                                        at com.track.client.GPSCheck.blowSiren(GPSCheck.java:35)
                                                                        at com.track.client.GPSCheck.onReceive(GPSCheck.java:30)
                                                                        at android.app.ActivityThread.handleReceiver(ActivityThread.java:2782)
                                                                        at android.app.ActivityThread.access$1800(ActivityThread.java:154) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1468) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                        at android.os.Looper.loop(Looper.java:234) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5526) 
                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Upvotes: 0

Views: 1270

Answers (2)

Markus Kauppinen
Markus Kauppinen

Reputation: 3235

Probably your this.context.getSystemService("audio"); call fails because the this.context is null.

You probably meant to set it to the class member in onReceive(Context context, Intent intent):

this.context = context;

But like the other answer points out it's instead better to pass it to stopSiren() and blowSiren() when calling those methods.

That's covered in the BroadcastReceiver documentation:

A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). Once your code returns from this function, the system considers the object to be finished and no longer active.

Upvotes: 0

The Science Boy
The Science Boy

Reputation: 359

First. I don't know if this is your problem but you should get AudioManager by calling using the constant Context.AUDIO_SERVICE, not rolling your own strings.

Second. Don't store the context, send it in via input parameter to blow and stop siren. Otherwise you might get freaky leaks (yep, you can leak memory in Java).

Dunno if this will fix your problem, but that's what I noticed right off anyway.

Upvotes: 2

Related Questions