• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Getting Runtime error during songshare function

JAVA null pointer exception no refference showing for the code

  • 1

    Votes: 0 0.0%
  • 0

    Votes: 0 0.0%

  • Total voters
    0

jos90876

Lurker
Feb 21, 2018
1
0
Code:
package com.sunny14.dabbu.nowplaying;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.sunny14.dabbu.MusicPlayer;
import com.sunny14.dabbu.R;
import com.sunny14.dabbu.models.Song;

import java.io.File;
import java.util.List;

public class Timber3 extends BaseNowplayingFragment {
    public List<Song> arraylist;
    int position = MusicPlayer.getQueuePosition();





    [USER=1021285]@override[/USER]
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(
                R.layout.fragment_timber3, container, false);

        ImageView we= (ImageView) rootView.findViewById(R.id.sharefor);

        we.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                shareSong(arraylist.get(position));////////RUN TIME ERROR SHOWING HERE
            }
        } );

        setMusicStateListener();
        setSongDetails(rootView);

        return rootView;


}
    private void shareSong(Song song)
    {
        try
        {
            Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            Cursor cur = mContext.getContentResolver().query(uri, null, MediaStore.Audio.Media._ID + " = "+song.id, null, null);
            cur.moveToNext();
            int dataColumn = cur.getColumnIndex(MediaStore.Audio.Media.DATA);
            String path = cur.getString(dataColumn);
            File f=new File(path);
            Uri uri1 = Uri.parse("file://"+f.getAbsolutePath());
            Intent share = new Intent(Intent.ACTION_SEND);
            share.putExtra(Intent.EXTRA_STREAM, uri1);
            share.setType("audio*//*");
            share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            mContext.startActivity(Intent.createChooser(share, "Share audio File"));
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

    }
 
Last edited by a moderator:
variable 'arraylist' is null. You need to do two things:-

1. Create a new instance of ArrayList

Code:
public List<Song> arraylist = new ArrayList<>();

2. You need to populate arraylist with Song objects. The code you show above does not appear to do this currently.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones