0

I was trying to pass an instance of my Music class from one activity to another, and I did get that to work; however, now I can't use any of Music class' methods in the second activity. It keeps throwing a NullPointerException when I try to call any method in Music.

Music Class:

public class Music implements Parcelable{

    private static ArrayList<genericSongClass> songs = new ArrayList<genericSongClass>();

    Cursor cursor;

    Context context;

    public Music(){};

    public Music(Context context){
        this.context = context;
    }

    public Music(Parcel in){
        songs = new ArrayList<genericSongClass>();
        in.readTypedList(songs, genericSongClass.CREATOR);
    }

    public void BindAllSongs() {        
            /** Making custom drawable */
            String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
            final String[] projection = new String[] {
                    MediaStore.Audio.Media.DISPLAY_NAME,
                    MediaStore.Audio.Media.ARTIST,
                    MediaStore.Audio.Media.DATA,
                    MediaStore.Audio.Media.ALBUM};
                    final String sortOrder = MediaStore.Audio.AudioColumns.TITLE
                            + " COLLATE LOCALIZED ASC";

                    try {
                        // the uri of the table that we want to query
                        Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                        // query the db
                        cursor = context.getContentResolver().query(uri,
                                projection, selection, null, sortOrder);
                        if (cursor != null) {
                            songs = new ArrayList<genericSongClass>(cursor.getCount());
                            cursor.moveToFirst();                       
                            while (!cursor.isAfterLast()) { 
                                genericSongClass GSC = new genericSongClass();
                                GSC.songTitle = cursor.getString(0);
                                GSC.songArtist = cursor.getString(1);   
                                GSC.songData = cursor.getString(2);
                                GSC.songAlbum = cursor.getString(3);
                                songs.add(GSC);
                                cursor.moveToNext();
                            }
                        }
                    } catch (Exception ex) {

                    } finally {
                        if (cursor != null) {
                            cursor.close();
                        }
                    }       

        }

    public static Object[] toArray(ArrayList<Object> list){
        Object[] toReturn = new Object[list.size()];
        for (int i = 0; i < list.size(); i++){
            toReturn[i] = list.get(i);
        }
        return toReturn;
    }

    public ArrayList<String> getArtists(){
        ArrayList<String> artists = new ArrayList<String>();
        for(genericSongClass gsc: songs){
            if(!artists.contains(gsc.songArtist)){
                artists.add(gsc.songArtist);
            }
        }
        Alphabetize forArtists = new Alphabetize(artists);
        return forArtists.getSortedArrayList();
    }

    public ArrayList<String> getAlbums(String artist){
        ArrayList<String> albums = new ArrayList<String>();
        Log.e("HEY!", "HEY!");
        for(genericSongClass gsc: songs){
            if(gsc.songArtist == artist){
                albums.add(gsc.songAlbum);
            }
        }
        Alphabetize forAlbums = new Alphabetize(albums);
        return forAlbums.getSortedArrayList();
    }

    //--- Parcel ------------------------------------------------

     public static final Parcelable.Creator<Music> CREATOR = new Parcelable.Creator<Music>() {  

            public Music createFromParcel(Parcel in) {  
                return new Music(in);  
            }

            @Override
            public Music[] newArray(int size) {
                return new Music[size];
            }  
        };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeList(songs);
    }
}

How I passed the instance of Music from activity one:

Intent in = new Intent(getApplicationContext(), Album_Activity.class);
    in.putExtra("Artist", artists.get(button.getId()));
    startActivity(in);
    in.putExtra("MusicInstance", MainActivity.this.thisInstance);

If I do startActivity after I put MusicInstance in the intent, I get a runtime error that says "unmarshalling unknown type."

How I received the instance in activity two:

Intent in = getIntent();
thisInstance = getIntent().getParcelableExtra("MusicInstance");
artist = (String) in.getExtras().get("Artist");

ArrayList<String> albums = thisInstance.getAlbums(artist);

The line

ArrayList<String> albums = thisInstance.getAlbums(artist); 

keeps throwing a NullPointerException. I've tried calling other methods from the class with the same error. I checked to make sure artist wasn't null.

Can anyone point me in the right direction here?

mtk
  • 13,221
  • 16
  • 72
  • 112
user2726232
  • 131
  • 11

1 Answers1

0

As you can see, you start the activity before you can put "MusicInstance", so you'll always get a NullPointerException.

I think you should try put a Serializable object


See the answer of this post

Community
  • 1
  • 1
Gabriel Câmara
  • 1,249
  • 15
  • 22