-4

Attempt to invoke virtual method 'void android.widget.ImageView.setImageDrawable(android.graphics.drawable.Drawable)'. on a null object reference. i m getting this error please help.

 private void loadSongs() {
        Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0";
        Cursor cursor = getContext().getContentResolver().query(uri, null, selection, null, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                do {

                    String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                    String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                    String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                    String albumId = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
                    Cursor cursorm = getContext().getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
                            new String[] {MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART},
                            MediaStore.Audio.Albums._ID+ "=?",
                            new String[] {String.valueOf(albumId)},
                            null);
                    assert cursorm != null;
                    if (cursorm.moveToFirst()) {
                        String path = cursorm.getString(cursorm.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
                        Drawable img = Drawable.createFromPath(path);
                        image = (ImageView)rootView.findViewById(R.id.albumArt);
                       // image.setImageDrawable(img);
                        image.setImageDrawable(ContextCompat.getDrawable(getActivity(),R.drawable.ed));
                    }
                    songInfo s = new songInfo(name, artist, url);
                    _songs.add(s);
                }
                while (cursor.moveToNext());
                cursor.close();
                recyclerView.getAdapter().notifyDataSetChanged();
            }
        }
    }

2 Answers2

1

It seems that this expression returns null:

image = (ImageView)rootView.findViewById(R.id.albumArt);

If image is null, then the next line raises an exception:

image.setImageDrawable(ContextCompat.getDrawable(getActivity(),R.drawable.ed));

You have to find out why (ImageView)rootView.findViewById(R.id.albumArt); returns null and guarantee that it does not return null OR check, if the image view is null and provide a default value for it.

gil.fernandes
  • 12,978
  • 5
  • 63
  • 76
0

As I understand method rootView.findViewById

returns

a view with given ID if found, or null otherwise

Implication of error msg:

view with given id has not been found therefore, image = (ImageView) null; and image is null;

Therefore while trying to execute:

image.setImageDrawable(..)

you get a NRE.

Solution:

assure that rootView.findViewById finds something.

PawelSz
  • 164
  • 6