0

When I run my code in Debug from Android Studio my app work good, but when I create the APK and install it on device and start my app, the app get NullPointerException error and get killed( using Android Debug Monitor). Here is the code:

public class MusicService extends IntentService {

    boolean INTERNET_CONNECTION = false;

    boolean COMMAND_DOWNALOAD_SDCARD = false;

    String TABLE_NAME = "";
    String COMMAND_ARGUMENT = "";

    // Here throw sometimes the error - Line 42
    AudioManager audio;

    MediaPlayer mp = new MediaPlayer();
    long TIME_ELAPSED = 0;
    long TIME_STARTED = 0;

    boolean ALIVE = true;
    long SLEEP_TIME = 5000;

    String SERVER_URL = "http://xxx.xxx";

    public MusicService() {
    super("Service");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
    Log.d("SERVICE", "STARTED");

    initialize();
    initializeTimer();

    while (ALIVE) {
        ...
    }
    }

    private void initialize() {
    // Get phone number set the table name
    TABLE_NAME = "t" + getPhoneNumber();

    audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

    // Wait for internet connection
    INTERNET_CONNECTION = waitInternetConnection();

    Log.d("INITIALIZE", "COMPLETED");
    }

    ...
}

I start my app when boot is completed. After some times of trying to run it I got error at line 42 and I changed the line from:

    AudioManager audio = null;

to

    AudioManager audio;

But still get the error but now don't show any more the line.

  • If that line is 42, it should not be throwing an error unless maybe something static in AudioManager is failing. I'd recompile and try again. – William Morrison Feb 04 '14 at 19:50

1 Answers1

0

There is no difference between these: AudioManager audio = null; and AudioManager audio;.

Changing the line from explicitly assigning null to the object doesn't change any functionality at all; all it means is that audio now implicitly defaults to null.

Clearly there is a difference in what getSystemService() does, depending on whether you're in your debugging environment versus your production environment. To find out what's wrong, go into that service. We don't have enough information here to debug it for you.

audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
asteri
  • 11,402
  • 13
  • 60
  • 84