I'm very new at Android/Java development, although I'm an experienced web dev. I've read this and this, and am still having problems.
I have a SQLite database with a table called Restaurants, that stores, well, restaurants. I'm trying to use a RecyclerView to display a list of restaurants.
I'm getting a null pointer exception, and can't seem to figure out what is null.
Here's my RecyclerView
class:
package com.myapp.lunchboxchicago.adapters;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.myapp.lunchboxchicago.Message;
import com.myapp.lunchboxchicago.R;
import com.myapp.lunchboxchicago.restaurants.Restaurant;
/**
* Created by estrom on 3/23/2016.
*/
public class RestaurantViewAdapter extends RecyclerView.Adapter<RestaurantViewAdapter.RestaurantViewHolder> {
public static final String TAG = RestaurantViewAdapter.class.getSimpleName();
private Restaurant[] mRestaurants;
private Context mContext;
public RestaurantViewAdapter(Context context, Restaurant[] restaurants) {
mContext = context;
mRestaurants = restaurants;
}
@Override
public RestaurantViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.restaurant_list_item, parent, false);
RestaurantViewHolder viewHolder = new RestaurantViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(RestaurantViewHolder holder, int position) {
holder.bindRestaurant(mRestaurants[position]);
}
@Override
public int getItemCount() {
return mRestaurants.length;
}
public class RestaurantViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView mRestaurantName;
public RestaurantViewHolder(View itemView) {
super(itemView);
mRestaurantName = (TextView) itemView.findViewById(R.id.restaurantNameLabel);
itemView.setOnClickListener(this);
}
public void bindRestaurant(Restaurant restaurant) {
Log.wtf(TAG, "Binding " + restaurant.getName());
Log.wtf(TAG, "To TextView ID: " + mRestaurantName.getId());
mRestaurantName.setText(restaurant.getName());
}
@Override
public void onClick(View v) {
Message.message(mContext, "We'd open " + mRestaurantName + " in the view screen");
}
}
}
The two Log
statements in bindRestaurant
return the following:
03-26 19:17:34.033 30414-30414/com.myapp.lunchboxchicago E/RestaurantViewAdapter: Binding Shamrock Club
03-26 19:17:34.050 30414-30414/com.myapp.lunchboxchicago E/RestaurantViewAdapter: To TextView ID: 2131558502
So it looks like both objects required exist and are instantiated. But I'm still getting this error on the very next line (mRestaurantName.setText(restaurant.getName());
)
03-26 19:17:34.057 30414-30414/com.myapp.lunchboxchicago D/AndroidRuntime: Shutting down VM
03-26 19:17:34.058 30414-30414/com.myapp.lunchboxchicago E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp.lunchboxchicago, PID: 30414
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.myapp.lunchboxchicago.restaurants.Restaurant.getName()' on a null object reference
at com.myapp.lunchboxchicago.adapters.RestaurantViewAdapter$RestaurantViewHolder.bindRestaurant(RestaurantViewAdapter.java:62)
at com.myapp.lunchboxchicago.adapters.RestaurantViewAdapter.onBindViewHolder(RestaurantViewAdapter.java:44)
at com.myapp.lunchboxchicago.adapters.RestaurantViewAdapter.onBindViewHolder(RestaurantViewAdapter.java:18)
I'm confused. What object am I missing that is null here?