aratata
aratata

Reputation: 1397

Why am i getting a nullpointerexception when I try to setAdapter?

I have a simple adapter that uses 2 String based array lists. For some reason I am getting a NullPointerException even though I am sure the information is being passed by.

My adapter:

public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder> {

    private ArrayList<String> names = new ArrayList<>();
    private ArrayList<String> details = new ArrayList<>();
    private Context mContext;

    public MovieAdapter(ArrayList<String> names, ArrayList<String> details, Context mContext) {
        this.names = names;
        this.details = details;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.detail_view, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {


        holder.name.setText(names.get(position));
        holder.detail.setText(details.get(position));
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{

        private TextView name;
        private TextView detail;
        private ConstraintLayout cl;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
            detail = itemView.findViewById(R.id.detial);
            cl = itemView.findViewById(R.id.layout);
        }
    }
}

My REST call and recylerView initialization activity(added the REST call in case the problem is lying there):

 //        ArrayList<String> namesArray = new ArrayList<>(Arrays.asList("Rated:", "Released:", "Runtime:", "Genre:", , "Writer:", "Actors:", "Plot:", "Language:", "Country:"));

    ArrayList<String> namesArray = new ArrayList<>();


            namesArray.add("Rated:");
            namesArray.add("Released:");
            namesArray.add("Runtime:");
            namesArray.add("Genre:");
            namesArray.add("Director:");
            namesArray.add("Writer:");
            namesArray.add("Actors:");
            namesArray.add("Plot");
            namesArray.add("Language:");
            namesArray.add("Country");


            trazi.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Call<Movie> call;

                    movieApi = ApiClient.getClient().create(MovieApi.class);

                    if (paramYear.getText().toString().matches("")) {
                        call = movieApi.getDetails(key, paramName.getText().toString(), null);
                    } else {
                        call = movieApi.getDetails(key, paramName.getText().toString(), Integer.parseInt(paramYear.getText().toString()));
                    }


                    call.enqueue(new Callback<Movie>() {
                        @Override
                        public void onResponse(Call<Movie> call, Response<Movie> response) {
                            Toast.makeText(getApplication().getApplicationContext(), " valja", Toast.LENGTH_LONG).show();

                            movie = response.body();


    //                        ArrayList<String> details = new ArrayList<>(Arrays.asList(movie.getRated(), movie.getReleased(), movie.getRuntime(), movie.getGenre(), movie.getDirector(), movie.getWriter(), movie.getActors(), movie.getPlot(), movie.getLanguage(), movie.getCountry()));

                            ArrayList<String> details = new ArrayList<>();
                            details.add(movie.getRated());
                            details.add(movie.getReleased());
                            details.add(movie.getRuntime());
                            details.add(movie.getGenre());
                            details.add(movie.getDirector());
                            details.add(movie.getWriter());
                            details.add(movie.getActors());
                            details.add(movie.getPlot());
                            details.add(movie.getLanguage());
                            details.add(movie.getCountry());

                            Picasso.get().load(movie.getPoster()).fit().centerInside().into(image);
                            image.setVisibility(View.VISIBLE);

                            title.setText(movie.getTitle());
                            title.setVisibility(View.VISIBLE);

                            recyclerView = findViewById(R.id.layout);
                            movieAdapter = new MovieAdapter(namesArray, details, getApplication().getApplicationContext());
                            recyclerView.setAdapter(movieAdapter);
                            recyclerView.setLayoutManager(new LinearLayoutManager(getApplication().getApplicationContext()));

                        }

So

Picasso.get().load(movie.getPoster()).fit().centerInside().into(image);

and

title.setText(movie.getTitle());

work perfectly so the information is passed to the adapter and I tried to initialize array lists on two different ways just in case. Still no success. Any ideas on where the problem is?

Edit: My error log:

2020-05-24 17:20:22.600 27595-27595/com.example.cetvrtizadatak E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.cetvrtizadatak, PID: 27595
    java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference
        at com.example.cetvrtizadatak.MainActivity$1$1.onResponse(MainActivity.java:115)
        at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1.lambda$onResponse$0$DefaultCallAdapterFactory$ExecutorCallbackCall$1(DefaultCallAdapterFactory.java:81)
        at retrofit2.-$$Lambda$DefaultCallAdapterFactory$ExecutorCallbackCall$1$3wC8FyV4pyjrzrYL5U0mlYiviZw.run(Unknown Source:6)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6702)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911)

Upvotes: 0

Views: 95

Answers (1)

Henrique
Henrique

Reputation: 26

Your NullPointerException is happening when you try to access a member of you recyclerView object, meaning that its value is probably null.

Looking at your code I can presume that the id you're passing is not of yout recyclerView but for an layout object.

Double check if in your layout xml file the id given to the recyclerView is the one you're referencing on you call to findViewById.

Upvotes: 1

Related Questions