I'm getting the following error :
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
This occurs at this line
holder.myTextView.setText(itemRecipe.get(position).toString());
I have gone through several troubleshooting steps for this and not sure what else to try or if the database is even working, the reason I want this to work.
What I have tried:
RecyclerAdapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private List<RecipeList> itemRecipe;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
RecyclerViewAdapter(Context context, List<RecipeList> data) {
this.mInflater = LayoutInflater.from(context);
this.itemRecipe = data;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.fragment_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.myTextView.setText(itemRecipe.get(position).toString());
}
@Override
public int getItemCount() {
return itemRecipe.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView myTextView;
ViewHolder(View itemView) {
super(itemView);
myTextView = itemView.findViewById(R.id.listIngredient);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}
void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
public interface ItemClickListener {
void onItemClick(View view, int position);
}
}
Main class
public class Recipe extends MainActivity {
TabLayout tabLayout;
ImageView recipeImage;
TextView descriptionText, courseText, servingsText, costText, caloriesText, methodText;
RecyclerView listIngredient;
SQLiteDatabase db;
String search_name;
Cursor c;
RecyclerViewAdapter adapterRecipe;
List<RecipeList> itemRecipe = new ArrayList<>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recipe);
search_name = "Speedy chicken couscous";
loadRecipe();
adapterRecipe = new RecyclerViewAdapter(this, itemRecipe);
listIngredient = findViewById(R.id.listIngredient);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this,
LinearLayoutManager.VERTICAL, false);
listIngredient.setLayoutManager(mLayoutManager);
listIngredient.setItemAnimator(new DefaultItemAnimator());
listIngredient.setAdapter(adapterRecipe);
}
public void loadRecipe() {
itemRecipe.clear();
db = (new DatabaseManager(this).getWritableDatabase());
String RECIPE_SEARCH = " SELECT A.recipe, A.ingredient_quantity, B.measurement_name, B.ingredient_name, B.description " +
"FROM " + DatabaseManager.TABLE_QUANTITY + " AS A JOIN " + DatabaseManager.TABLE_INGREDIENTS +
" AS B ON A.ingredient = B.ingredient_name";
String selectQuery = "";
selectQuery = RECIPE_SEARCH + " WHERE A.recipe LIKE ?";
c = db.rawQuery(selectQuery, new String[]{"%" + search_name + "%"});
if (c.moveToFirst()) {
do {
RecipeList recipeList = new RecipeList();
recipeList.setRecipe(c.getString(c.getColumnIndex("recipe")));
recipeList.setIngredient_amount(c.getString(c.getColumnIndex("ingredient_quantity")));
recipeList.setMeasurement_name(c.getString(c.getColumnIndex("measurement_name")));
recipeList.setIngredient_name(c.getString(c.getColumnIndex("ingredient_name")));
recipeList.setDescription(c.getString(c.getColumnIndex("description")));
itemRecipe.add(recipeList);
} while (c.moveToNext());
c.close();
}
}
}
RecipeList Class
public class RecipeList {
private Integer id;
private Double ingredient_quantity;
private String recipe;
private String measurement_name;
private String ingredient_name;
private String description;
private String ingredient_amount = String.valueOf(ingredient_quantity);
public RecipeList(){
}
public RecipeList(String recipe, String ingredient_amount, String measurement_name, String ingredient_name, String description) {
this.recipe = recipe;
this.ingredient_amount = ingredient_amount;
this.measurement_name = measurement_name;
this.ingredient_name = ingredient_name;
this.description = description;
}
public String getRecipe() {
return recipe;
}
public void setRecipe(String recipe) {
this.recipe = recipe;
}
public String getIngredient_amount() {
return ingredient_amount;
}
public void setIngredient_amount(String ingredient_amount) {
this.ingredient_amount = ingredient_amount;
}
public String getMeasurement_name() {
return measurement_name;
}
public void setMeasurement_name(String measurement_name) {
this.measurement_name = measurement_name;
}
public String getIngredient_name() {
return ingredient_name;
}
public void setIngredient_name(String ingredient_name) {
this.ingredient_name = ingredient_name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "RecipeList{" +
"id=" + id +
", ingredient_quantity=" + ingredient_quantity +
", recipe='" + recipe + '\'' +
", measurement_name='" + measurement_name + '\'' +
", ingredient_name='" + ingredient_name + '\'' +
", description='" + description + '\'' +
", ingredient_amount='" + ingredient_amount + '\'' +
'}';
}
}