Using ContentResolver to report errors in service

problem description

using ContentResolver to report an error in service, the error is as follows, how to solve it?

error

FATAL EXCEPTION: main
Process: com.mp.mmedicaplayer, PID: 16990
  java.lang.RuntimeException: Unable to instantiate service com.mp.mmedicaplayer.service.MusicService: java.lang.NullPointerException: Attempt to invoke virtual method "android.content.ContentResolver android.content.Context.getContentResolver()" on a null object reference
  at android.app.ActivityThread.handleCreateService(ActivityThread.java:3390)
  at android.app.ActivityThread.-wrap5(ActivityThread.java)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1753)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:159)
  at android.app.ActivityThread.main(ActivityThread.java:6385)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1096)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:883)
   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method "android.content.ContentResolver android.content.Context.getContentResolver()" on a null object reference
  at com.mp.mmedicaplayer.service.MusicService.getMusicToArray(MusicService.java:211)
  at com.mp.mmedicaplayer.service.MusicService.<init>(MusicService.java:48)
  at java.lang.Class.newInstance(Native Method)
  at android.app.ActivityThread.handleCreateService(ActivityThread.java:3387)
  at android.app.ActivityThread.-wrap5(ActivityThread.java)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1753)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:159)
  at android.app.ActivityThread.main(ActivityThread.java:6385)
  at java.lang.reflect.Method.invoke(Native Method)

Code

public void getMusicToArray(){
        /**
         * 
         *
         */
        ContentResolver contentResolver = getContentResolver();
        Cursor cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,null,null,MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
        List<MusicListData> mListData = new ArrayList<>();
        //Cursor
        if(cursor.moveToFirst()){
            for(int i = 0; i <cursor.getCount();iPP) {
                MusicListData data = new MusicListData();
                long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID));
                String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
                String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
                long size = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE));
                String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                String abum = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
                long albulm_id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
                int ismusic = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));
                Log.d("MMMM",url);

                if (ismusic != 0 && duration / (500 * 60) >= 1) {
                    Log.d("M","2");

                    musicPath[i] = url;

                    data.setTitle(title);
                    data.setUrl(url);
                    data.setSonger(artist);
                    mListData.add(data);
                }
                //
                cursor.moveToNext();
            }

        }
        //
        cursor.close();
    }
Apr.13,2022

getContentResolver () requires context, try getApplicationContext (). GetContentResolver ()


Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver ()' on a null object reference
at com.mp.mmedicaplayer.service.MusicService.getMusicToArray (MusicService.java:211)
at com.mp.mmedicaplayer.service.MusicService. < init > (MusicService.java:48)

from the error message above, you can see that getMusicToArray () is called in the constructor of Service , and the call that causes the error is Service-sharpgetContentResolver () . Look at the Android source code:

public abstract class Service extends ContextWrapper implements ComponentCallbacks2 {
    ... ...
}

public class ContextWrapper extends Context {
    Context mBase;

    protected void attachBaseContext(Context base) {
        if (mBase != null) {
            throw new IllegalStateException("Base context already set");
        }
        mBase = base;
    }

    @Override
    public ContentResolver getContentResolver() {
        return mBase.getContentResolver();
    }
}

Service-sharpgetContentResolver () finally calls mBase.getContentResolver () , while the mBase assignment method attachBaseContext (Context) is operated by the system according to the life cycle, and naturally does not precede the constructor. Calling Context related functions in the constructor will also naturally report an error.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b2bb9f-40045.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b2bb9f-40045.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?