2

I am trying to create an intent with bundle extras but I am getting null pointer exception error on the Array List.

Here is the error:

     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList android.os.Bundle.getParcelableArrayList(java.lang.String)' on a null object reference

Am I implementing intents wrong?

Here is my application below for MP3 Player:

PlaylistSelector:

public class PlaylistSelector extends AppCompatActivity {
    ListView listview;
    String[] items;



    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.playlist);

        listview = findViewById(R.id.listViewSong);

        runtimePermission();
    }

    public void runtimePermission()
    {
        Dexter.withContext(this).withPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
                .withListener(new PermissionListener() {
                                  @Override
                                  public void onPermissionGranted(PermissionGrantedResponse permissionGrantedResponse) {
                                      displaySongs();


                                  }

                                  @Override
                                  public void onPermissionDenied(PermissionDeniedResponse permissionDeniedResponse) {

                                  }

                                  @Override
                                  public void onPermissionRationaleShouldBeShown(PermissionRequest permissionRequest, PermissionToken permissionToken) {
                                      permissionToken.continuePermissionRequest();

                                  }
                }).check();
    }

    public ArrayList<File> findSong (File file)
    {
        ArrayList<File> arrayList = new ArrayList<>();
        File[] files = file.listFiles();

        for (File singlefile: files)
        {
            if (singlefile.isDirectory() && !singlefile.isHidden())
            {
                arrayList.addAll(findSong(singlefile));
            }
            else
            {
                if (singlefile.getName().endsWith(".mp3") || singlefile.getName().endsWith(".wav"))
                {
                    arrayList.add(singlefile);
                }
            }
        }
        return arrayList;
    }

    void displaySongs()
    {
        final ArrayList<File> mySongs = findSong(Environment.getExternalStorageDirectory());
        items = new String[mySongs.size()];
        for(int i = 0; i<mySongs.size(); i++)
        {
            items[i] = mySongs.get(i).getName().toString().replace(".mp3","").replace(".wav","");

        }
        /*ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,songs);
        listview.setAdapter(myAdapter);*/

        customAdapter customAdapter = new customAdapter();
        listview.setAdapter(customAdapter);


        listview.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String songName = (String) listview.getItemAtPosition(i);
                startActivity(new Intent(getApplicationContext(), MainActivity.class)
                        .putExtra("songs", mySongs)
                        .putExtra("songname", songName)
                        .putExtra("pos", i));
            }
            });
    }


    class customAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return items.length;
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View myView = getLayoutInflater().inflate(R.layout.list_item, null);
            TextView textsong = myView.findViewById(R.id.txtsongname);
            textsong.setSelected(true);
            textsong.setText(items[i]);

            return myView;
        }
    }
}

MainActivity:

public class MainActivity extends AppCompatActivity  {
  private static final String TAG = "MainActivity";

  private InputSource inputSource;




  

  Button playbtn, btnnext,btnprev,btnff,btnrw;
  TextView txtsn, txtsstop,txtsstart;
  SeekBar seekmusic;
  BarVisualizer visualizer;

  String sname;

  public static final String EXTRA_NAME = "song_name";
  static MediaPlayer mediaPlayer;
  int position;
  ArrayList<File> mySongs;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    View frameLayoutMP = findViewById(R.id.preview_mp3_layout);
    btnprev = findViewById(R.id.btnprev);
    playbtn = findViewById(R.id.playbtn);
    btnnext = findViewById(R.id.btnnext);
    btnrw = findViewById(R.id.btnrw);
    btnff = findViewById(R.id.btnff);
    txtsn = findViewById(R.id.txtsn);
    txtsstop = findViewById(R.id.txtsstop);
    txtsstart = findViewById(R.id.txtsstart);
    seekmusic = findViewById(R.id.seekbar);
    visualizer = findViewById(R.id.blast);

    if (mediaPlayer != null)
    {
      mediaPlayer.stop();
      mediaPlayer.release();
    }

    Intent i = getIntent();
    Bundle bundle = i.getExtras();

    mySongs = (ArrayList) bundle.getParcelableArrayList("songs");
    String songName = i.getStringExtra("songname");
    position = bundle.getInt("pos",0);
    txtsn.setSelected(true);
    Uri uri = Uri.parse(mySongs.get(position).toString());
    sname = mySongs.get(position).getName();
    txtsn.setText(sname);

    mediaPlayer = MediaPlayer.create(getApplicationContext(),uri);
    mediaPlayer.start();

    playbtn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        if(mediaPlayer.isPlaying())
        {
          playbtn.setBackgroundResource(R.drawable.ic_play);
          mediaPlayer.pause();
        }
        else
        {
          playbtn.setBackgroundResource(R.drawable.ic_pause);
          mediaPlayer.start();
        }
      }
    });


}


}

I am trying to get this intent to grab song lists from playlists and put it into the player

devman3211
  • 63
  • 1
  • 11
  • final ArrayList mySongs = findSong(Environment.getExternalStorageDirectory());, the arraylist seem to be null! Check whether your mySongs ArrayList is not null. if(mySongs != null) – The Dongster Mar 27 '22 at 18:53
  • `MainActivity` is getting started with an `Intent` that does not contain "extras". Is `MainActivity` started when you launch your app? Please edit your question and add your manifest to the post. – David Wasser Mar 28 '22 at 11:52
  • @TheDongster No. The NPE says that the `Bundle` is `null`. Not that the "extra" in the `Bundle` is null. – David Wasser Mar 28 '22 at 11:53
  • @DavidWasser No well what i think is that since the arraylist is null or the type of arraylist is not parcelable, it will produce nullpointerexception since intent cannot pass the arraylist. – The Dongster Mar 28 '22 at 14:30
  • 1
    @TheDongster Nope. OP posted the exception. It clearly states that he is trying to call `getParcelableArrayList()` on a `null` reference. If you look at the code, he is calling `getParcelableArrayList()` on a variable of type `Bundle` which was returned from the call to `Intent.getExtras()`. So clearly, the `Bundle` is `null`. If the problem was occuring while trying to add the `ArrayList` to the `Intent`, the exception would have been thrown in a different place. – David Wasser Mar 28 '22 at 17:38
  • @DavidWasser You were right. Sorry. Your' "MainActivity is getting started with an Intent that does not contain "extras"" is the correct answer! :-) – The Dongster Mar 28 '22 at 18:08
  • So the possible solution in that situation can be checking null for the bundle. if(bundle!=null). – The Dongster Mar 28 '22 at 18:09

0 Answers0