How to pass data on image click from RecycleAdapter to Fragment interface, which I used in MainActivity to show data
<p>I am trying to archive a data from onClick method of RecycleAdapter class to Fragment Class and that fragement class have interface which I used in MainActivity to get that data. I create a method onButtonPressed() in Fragment class which I called in recycleadapter onclick method for get data.
But when I click on any image, it shows NullPointerException error : </p>

<pre><code>04-17 03:24:56.376 6507-6545/com.santossingh.moviesinfoapp E/Surface: getSlotFromBufferLocked: unknown buffer: 0xf3d958e0
04-17 03:24:58.211 6507-6507/com.santossingh.moviesinfoapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.santossingh.moviesinfoapp, PID: 6507
                                                                             java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
                                                                                 at android.widget.Toast.&lt;init&gt;(Toast.java:102)
                                                                                 at android.widget.Toast.makeText(Toast.java:259)
                                                                                 at com.santossingh.moviesinfoapp.Fragments.Fragment_main.onButtonPressed(Fragment_main.java:176)
                                                                                 at com.santossingh.moviesinfoapp.Adapters.RecyclerAdapter$Holder.onClick(RecyclerAdapter.java:89)
                                                                                 at android.view.View.performClick(View.java:5198)
                                                                                 at android.view.View$PerformClick.run(View.java:21147)
                                                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 at android.os.Looper.loop(Looper.java:148)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
</code></pre>

<p>Here is my code 
1) Main Activity class</p>

<pre><code>public class MainActivity extends AppCompatActivity implements Fragment_main.OnFragmentInteractionListener{

    private Realm realm;
    private Results results;
    RecyclerAdapter recyclerAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this).build();
        Realm.setDefaultConfiguration(realmConfiguration);

    }

    @Override
    public void onFragmentInteraction(Results currentMovie) {
        if (currentMovie==null){
            Toast.makeText(this,"Null",Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this,"Hi ",Toast.LENGTH_SHORT).show();
        }
        DetailFragment detailFragment = (DetailFragment) getFragmentManager()
                .findFragmentById(R.id.Detail_container);

        if (detailFragment == null) {
            Toast.makeText(this,"single view",Toast.LENGTH_SHORT).show();

            Intent intent = new Intent(this, DetailActivity.class)
                .putExtra("movie_Id", currentMovie.getId())
                .putExtra("movie_Name", currentMovie.getTitle())
                .putExtra("poster_Path", currentMovie.getPoster_path())
                .putExtra("back_poster_Path", currentMovie.getBackdrop_path())
                .putExtra("release_Date", currentMovie.getRelease_date())
                .putExtra("users_Rating", currentMovie.getVote_average())
                .putExtra("overview", currentMovie.getOverview());
            startActivity(intent);

        } else {
            detailFragment.UpdateTabletUI(currentMovie);
        }
    }
}
</code></pre>

<p>2) Fragment_main class
&lt;* After correction *>************</p>

<pre><code>public class Fragment_main extends Fragment implements RecyclerAdapter.GetDataFromAdapter {

    @Bind(R.id.recyclerView) RecyclerView recyclerView;
    private static final String STATE_MOVIES = "state_movies";
    private View rootView = null;
    private ServiceManager serviceManager;
    private RecyclerAdapter recyclerAdapter;
    private MovieResponse movieResponse;
    private List&lt;Results&gt; resultsList;

    private Results results;
    private OnFragmentInteractionListener mListener;
    private RecyclerAdapter.GetDataFromAdapter getDataFromAdapter;

    public Fragment_main() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelableArrayList(STATE_MOVIES, (ArrayList&lt;? extends Parcelable&gt;) resultsList);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.High_rated:
                if(item.isChecked()){
                    item.setChecked(false);
                }else{
                    item.setChecked(true);
                    makeService("TOP");
                    return true;
                }
            case R.id.most_Popular:
                if(item.isChecked()){
                    item.setChecked(false);
                }else{
                    item.setChecked(true);
                    makeService("POPULAR");
                    return true;
                }
            case R.id.favoriteList:
                if(item.isChecked()){
                    item.setChecked(false);
                }else{
                    item.setChecked(true);
                    Intent intent=new Intent(getActivity(), FavoriteActivity.class);
                    startActivity(intent);
                    return true;
                }
        }
        return super.onOptionsItemSelected(item);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_main, container, false);

            ButterKnife.bind(this, rootView);
            resultsList=new ArrayList&lt;Results&gt;();
            configRecycleView();

            if (savedInstanceState==null){
                makeService("POPULAR");
            }else{
                resultsList=savedInstanceState.getParcelableArrayList(STATE_MOVIES);
                recyclerAdapter.addMovieList(resultsList);
            }
        return rootView;
    }

    private void makeService(String query){
        serviceManager = new ServiceManager();
        Call&lt;MovieResponse&gt; listCall;
            if (query=="TOP") {
                listCall= serviceManager.getJSONData().getTopMovies();
            }else{
                listCall= serviceManager.getJSONData().getPopMovies();
            }
            listCall.enqueue(new Callback&lt;MovieResponse&gt;() {
                @Override
                public void onResponse(Call&lt;MovieResponse&gt; call, Response&lt;MovieResponse&gt; response) {
                    if (response.isSuccessful()) {
                        Results[] results = response.body().getResults();
                        resultsList = new ArrayList&lt;Results&gt;(Arrays.asList(results));
                        recyclerAdapter.addMovieList(resultsList);
                        Toast.makeText(getActivity(), "Data Fatch", Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFailure(Call&lt;MovieResponse&gt; call, Throwable t) {
                    Toast.makeText(getActivity(), t.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
    }

    private void configRecycleView() {
       recyclerView.setHasFixedSize(true);
       recyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());
       recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),
               2));
       recyclerAdapter = new RecyclerAdapter(getActivity(),getDataFromAdapter
       );
       recyclerView.setAdapter(recyclerAdapter);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onCurrentMovie(Results currentMovie) {
        mListener.onFragmentInteraction(currentMovie);
    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Results result);
    }

}
</code></pre>

<hr>

<p>3) RecycleAdapter class</p>

<p>public class RecyclerAdapter extends RecyclerView.Adapter{
    private List resultsList=new ArrayList();
    private View rcView;
    private Context context;
    int preposition;</p>

<pre><code>GetDataFromAdapter callback;
private Results currentMovie;

public interface GetDataFromAdapter{
    void onCurrentMovie(Results currentMovie);
}

public RecyclerAdapter(Context context, GetDataFromAdapter callback) {
    this.context=context;
    this.callback = callback;
}

@Override
// create View object and pass it on Holder class constructor
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
    rcView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_image_fragment, parent, false);
    return new Holder(rcView);
}

@Override
public int getItemCount() {
    return resultsList.size();
}

public void addMovieList(List&lt;Results&gt; movie){
    resultsList=movie;
    notifyDataSetChanged();
}

@Override
public void onBindViewHolder(Holder holder, int position) {
    Results currentMovie = resultsList.get(position);
    Picasso.with(holder.imageView.getContext())
            .load(Constant.HTTP.IMAGE_BASE_URL + currentMovie.getPoster_path())
            .resize(240, 330)
            .placeholder(R.drawable.placeholder)
            .error(R.drawable.error)
            .into(holder.imageView);

    // animation part ----------------
    if (position&gt;preposition){
        AnimationUtil.animate(holder, true);
    }else {
        AnimationUtil.animate(holder,false);
    }
    preposition=position;
}

public class Holder extends RecyclerView.ViewHolder implements View.OnClickListener{
    private ImageView imageView;

    public Holder(View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);
        imageView=(ImageView)itemView.findViewById(R.id.imageView);
    }

    @Override
    public void onClick(View v) {
        int position = getAdapterPosition();
        currentMovie=resultsList.get(position);
        Toast.makeText(v.getContext(),currentMovie.getTitle(),Toast.LENGTH_SHORT).show();
        callback.onCurrentMovie(currentMovie);
    }
}
</code></pre>

<p>}
 After correction it again shows NullPointerException :
here is the error</p>

<pre><code>04-17 04:19:21.832 7118-7147/com.santossingh.moviesinfoapp E/Surface: getSlotFromBufferLocked: unknown buffer: 0xdefa22a0
04-17 04:19:22.633 7118-7118/com.santossingh.moviesinfoapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.santossingh.moviesinfoapp, PID: 7118
                                                                             java.lang.NullPointerException: Attempt to invoke interface method 'void com.santossingh.moviesinfoapp.Adapters.RecyclerAdapter$GetDataFromAdapter.onCurrentMovie(com.santossingh.moviesinfoapp.DB.Models.Results)' on a null object reference
                                                                                 at com.santossingh.moviesinfoapp.Adapters.RecyclerAdapter$Holder.onClick(RecyclerAdapter.java:92)
                                                                                 at android.view.View.performClick(View.java:5198)
                                                                                 at android.view.View$PerformClick.run(View.java:21147)
                                                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 at android.os.Looper.loop(Looper.java:148)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
</code></pre>

<p>After correction a single error shows and images not show in emulator and that is pic of emulator:</p>

<p><a href="http://i.stack.imgur.com/sIwvi.jpg" rel="nofollow">enter image description here</a></p>

<pre><code>04-17 04:54:16.961 23492-23520/com.santossingh.moviesinfoapp E/Surface: getSlotFromBufferLocked: unknown buffer: 0xeb08a7e0
</code></pre>

<p>both  classes code here :
Fragment class </p>

<pre><code>public class Fragment_main extends Fragment implements RecyclerAdapter.GetDataFromAdapter {

    @Bind(R.id.recyclerView) RecyclerView recyclerView;
    private static final String STATE_MOVIES = "state_movies";
    private View rootView = null;
    private ServiceManager serviceManager;
    private RecyclerAdapter recyclerAdapter;
    private MovieResponse movieResponse;
    private List&lt;Results&gt; resultsList;

    private Results results;
    private OnFragmentInteractionListener mListener;
    private RecyclerAdapter.GetDataFromAdapter getDataFromAdapter;

    public Fragment_main() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelableArrayList(STATE_MOVIES, (ArrayList&lt;? extends Parcelable&gt;) resultsList);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.High_rated:
                if(item.isChecked()){
                    item.setChecked(false);
                }else{
                    item.setChecked(true);
                    makeService("TOP");
                    return true;
                }
            case R.id.most_Popular:
                if(item.isChecked()){
                    item.setChecked(false);
                }else{
                    item.setChecked(true);
                    makeService("POPULAR");
                    return true;
                }
            case R.id.favoriteList:
                if(item.isChecked()){
                    item.setChecked(false);
                }else{
                    item.setChecked(true);
                    Intent intent=new Intent(getActivity(), FavoriteActivity.class);
                    startActivity(intent);
                    return true;
                }
        }
        return super.onOptionsItemSelected(item);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_main, container, false);

            ButterKnife.bind(this, rootView);
            resultsList=new ArrayList&lt;Results&gt;();
            configRecycleView();

            if (savedInstanceState==null){
                makeService("POPULAR");
            }else{
                resultsList=savedInstanceState.getParcelableArrayList(STATE_MOVIES);
                recyclerAdapter.addMovieList(resultsList);
            }
        return rootView;
    }

    private void makeService(String query){
        serviceManager = new ServiceManager();
        Call&lt;MovieResponse&gt; listCall;
            if (query=="TOP") {
                listCall= serviceManager.getJSONData().getTopMovies();
            }else{
                listCall= serviceManager.getJSONData().getPopMovies();
            }
            listCall.enqueue(new Callback&lt;MovieResponse&gt;() {
                @Override
                public void onResponse(Call&lt;MovieResponse&gt; call, Response&lt;MovieResponse&gt; response) {
                    if (response.isSuccessful()) {
                        Results[] results = response.body().getResults();
                        resultsList = new ArrayList&lt;Results&gt;(Arrays.asList(results));
                        recyclerAdapter.addMovieList(resultsList);
                        Toast.makeText(getActivity(), "Data Fatch", Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFailure(Call&lt;MovieResponse&gt; call, Throwable t) {
                    Toast.makeText(getActivity(), t.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
    }

    private void configRecycleView() {
       recyclerView.setHasFixedSize(true);
       recyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());
       recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),
               2));
       recyclerAdapter = new RecyclerAdapter(getActivity(),getDataFromAdapter);
       recyclerView.setAdapter(recyclerAdapter);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onCurrentMovie(Results currentMovie) {
        mListener.onFragmentInteraction(currentMovie);
    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Results result);
    }

}
</code></pre>

<p>RecycleAdapter class</p>

<pre><code>public class RecyclerAdapter extends RecyclerView.Adapter&lt;RecyclerAdapter.Holder&gt;{
    private List&lt;Results&gt; resultsList=new ArrayList&lt;Results&gt;();
    private View rcView;
    private Context context;
    int preposition;

    GetDataFromAdapter callback;
    private Results currentMovie;

    public interface GetDataFromAdapter{
        void onCurrentMovie(Results currentMovie);
    }

    public RecyclerAdapter(Context context, GetDataFromAdapter callback) {
        this.context=context;
        this.callback = callback;
    }

    @Override
    // create View object and pass it on Holder class constructor
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        rcView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.list_image_fragment, parent, false);
        return new Holder(rcView, callback);
    }

    @Override
    public int getItemCount() {
        return resultsList.size();
    }

    public void addMovieList(List&lt;Results&gt; movie){
        resultsList=movie;
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        Results currentMovie = resultsList.get(position);
        Picasso.with(holder.imageView.getContext())
                .load(Constant.HTTP.IMAGE_BASE_URL + currentMovie.getPoster_path())
                .resize(240, 330)
                .placeholder(R.drawable.placeholder)
                .error(R.drawable.error)
                .into(holder.imageView);

        // animation part ----------------
        if (position&gt;preposition){
            AnimationUtil.animate(holder, true);
        }else {
            AnimationUtil.animate(holder,false);
        }
        preposition=position;
    }

    public class Holder extends RecyclerView.ViewHolder implements View.OnClickListener{
        private ImageView imageView;
        private GetDataFromAdapter callback;

        public Holder(View itemView, GetDataFromAdapter callback) {
            super(itemView);
            itemView.setOnClickListener(this);
            this.callback=callback;
            imageView=(ImageView)itemView.findViewById(R.id.imageView);
        }

        @Override
        public void onClick(View v) {
            int position = getAdapterPosition();
            currentMovie=resultsList.get(position);
            Toast.makeText(v.getContext(),currentMovie.getTitle(),Toast.LENGTH_SHORT).show();
            callback.onCurrentMovie(currentMovie);
        }
    }

}
</code></pre>

<hr>

<p>Again NullException in Fragment class method and RecycleAdapter onclick method. </p>

<blockquote>
  <p>I test it via toast. Data comes in Fragement but erro shows when it send
  from fragment interface mListener to main Activity which called in
  callback method.</p>
</blockquote>

<pre><code>@Override
    public void onCurrentMovie(Results currentMovie) {
        mListener.onFragmentInteraction(currentMovie);
    }
</code></pre>

<p>and RecycleAdapter class onclick method line </p>

<pre><code>   callback.onCurrentMovie(currentMovie);
</code></pre>

<p>Error logcat :</p>

<pre><code>FATAL EXCEPTION: main
                                                                             java.lang.NullPointerException
                                                                                 at com.santossingh.moviesinfoapp.Fragments.Fragment_main.onCurrentMovie(Fragment_main.java:173)
                                                                                 at com.santossingh.moviesinfoapp.Adapters.RecyclerAdapter$Holder.onClick(RecyclerAdapter.java:93)
                                                                                 at android.view.View.performClick(View.java:4084)
                                                                                 at android.view.View$PerformClick.run(View.java:16966)
                                                                                 at android.os.Handler.handleCallback(Handler.java:615)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                                 at android.os.Looper.loop(Looper.java:137)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:4745)
                                                                                 at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                 at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                                                 at dalvik.system.NativeStart.main(Native Method)
</code></pre>