2

I have a recycler view which contains multiple items and each item in the recycler view contains a horizontal recycler view.The problem I am encountering is that the layout manager is null.

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference

This is my code so far.I have checked that the data I am receiving is intact.

RecipeAdapter ( The main adapter )

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

private Context context;
private List<Object> items;

private final int RECIPE = 0, JOKE = 1;

public RecipeAdapter(Context context) {
    this.context = context;
    this.items = new ArrayList<>();
}

public void setItems(List<Object> items) {
    this.items = items;
}

public void add(Object object) {
    items.add(object);
    notifyItemInserted(items.size());
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RecyclerView.ViewHolder viewHolder;
    final LayoutInflater inflater = LayoutInflater.from(parent.getContext());

    switch (viewType) {
        case RECIPE:
            View recipe = inflater.inflate(R.layout.item_recipe, parent, false);
            viewHolder = new ViewHolder_Recipe(recipe);
            break;
        case JOKE:
            View joke = inflater.inflate(R.layout.item_joke, parent, false);
            viewHolder = new ViewHolder_Joke(joke);
            break;
        default:
            View recipe_default = inflater.inflate(R.layout.item_recipe, parent, false);
            viewHolder = new ViewHolder_Recipe(recipe_default);
            break;
    }
    return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    switch (holder.getItemViewType()) {
        case RECIPE:
            ViewHolder_Recipe viewHolderRecipe = (ViewHolder_Recipe) holder;
            configureRecipeHolder(viewHolderRecipe, position);
            break;
        case JOKE:
            ViewHolder_Joke viewHolderJoke = (ViewHolder_Joke) holder;
            configureJokeHolder(viewHolderJoke, position);
            break;
        default:
            ViewHolder_Recipe viewHolder_recipe_default = (ViewHolder_Recipe) holder;
            configureRecipeHolder(viewHolder_recipe_default, position);
            break;
    }
}

private void configureJokeHolder(ViewHolder_Joke viewHolderJoke, int position) {

}

private void configureRecipeHolder(ViewHolder_Recipe viewHolderRecipe, int position) {

    RecipeDetailed recipe = (RecipeDetailed) items.get(position);

    Glide.with(context)
            .load(recipe.getImage())
            .into(viewHolderRecipe.getRecipe_image());

    viewHolderRecipe.getRecipe_name().setText(recipe.getTitle());
    viewHolderRecipe.getRecipe_prep().setText(recipe.getReadyInMinutes());
    viewHolderRecipe.getRecipe_serves().setText(recipe.getServings());

    viewHolderRecipe.getIngredientAdapter().setIngredients(recipe.getExtendedIngredients());
}

@Override
public int getItemViewType(int position) {
    if (items.get(position) instanceof RecipeDetailed) {
        return RECIPE;
    } else if (items.get(position) instanceof Joke) {
        return JOKE;
    }

    return super.getItemViewType(position);
}

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

}

The ViewHolder for that Adapter -- ViewHolder_Recipe

public class ViewHolder_Recipe extends RecyclerView.ViewHolder {

private CircularImageView recipe_image;
private TextView recipe_name;
private TextView recipe_prep;
private TextView recipe_serves;
private RecyclerView recyclerView;
private RecipeIngredientAdapter ingredientAdapter;

public ViewHolder_Recipe(View itemView) {
    super(itemView);
    recipe_image = (CircularImageView) itemView.findViewById(R.id.recipe_image);
    recipe_name = (TextView) itemView.findViewById(R.id.recipe_name);
    recipe_prep = (TextView) itemView.findViewById(R.id.recipe_prep);
    recipe_serves = (TextView) itemView.findViewById(R.id.recipe_serves);
    recyclerView = (RecyclerView) itemView.findViewById(R.id.recyclerView);

    ingredientAdapter = new RecipeIngredientAdapter(itemView.getContext());
    recyclerView.setLayoutManager(new LinearLayoutManager(itemView.getContext()
            , LinearLayoutManager.HORIZONTAL, false));
    recyclerView.setAdapter(ingredientAdapter);
}

public RecipeIngredientAdapter getIngredientAdapter() {
    return ingredientAdapter;
}

public CircularImageView getRecipe_image() {
    return recipe_image;
}

public TextView getRecipe_name() {
    return recipe_name;
}

public TextView getRecipe_prep() {
    return recipe_prep;
}

public TextView getRecipe_serves() {
    return recipe_serves;
}

public RecyclerView getRecyclerView() {
    return recyclerView;
}

}

The child adapter -- RecipeIngredientAdapter

public class RecipeIngredientAdapter extends RecyclerView.Adapter<ViewHolderRecipeIngredient> {

private Context context;
private ArrayList<Ingredient> ingredients;

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

public void setIngredients(ArrayList<Ingredient> ingredients) {
    this.ingredients = ingredients;
}

@Override
public ViewHolderRecipeIngredient onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View view = inflater.inflate(R.layout.item_recipe_ingredients, parent, false);
    return new ViewHolderRecipeIngredient(view);
}

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

    final Ingredient ingredient = ingredients.get(position);
    Glide.with(context)
            .load(ingredient.getImage())
            .into(holder.getIngredient_image());

}

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

}

The viewholder for that is a simple viewholder which contains an image. I have seen posts online which seem to do the exact same thing and get it working,what exactly am i missing here?

Jude Fernandes
  • 6,969
  • 9
  • 43
  • 82
  • Along with the wrong id that i was submitting to the recycler view,i also forgot to change the getItemCount() in the adapter to reflect the list size so it was always 0 and the adapter would never show any data. – Jude Fernandes Dec 20 '16 at 16:11

1 Answers1

3

From the error, it sounds like the RecyclerView is null, not the LayoutManager. Make sure you are using the proper id from the layout. are you sure the proper layout id of the RecyclerView is R.id.recyclerView?

  • I managed to fix that but the child recycler view just refuses to show anything even though data is there,it doesn't show up. – Jude Fernandes Dec 16 '16 at 14:48
  • You never pass data into the IngredientsAdapter with your setIngredients method. Also, with adapters, you typically want to initialize data by passing it into the constructor. So change IngredientsAdapter's constructor to take 2 arguments, context and the ArrayList of data. – Brian J. Roberts Dec 16 '16 at 21:44