无法确定NullPointerException中哪个变量为null

时间:2016-03-27 00:31:01

标签: java android

我是Android / Java开发的新手,尽管我是一位经验丰富的网络开发人员。我看过thisthis,但仍然遇到问题。

我有一个SQLite数据库,里面有一个名为Restaurants的表,可存储餐馆。我正在尝试使用RecyclerView来显示餐馆列表。

我得到一个空指针异常,似乎无法弄清楚什么是null。

这是我的RecyclerView课程:

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");
        }
    }
}

Log中的两个bindRestaurant语句返回以下内容:

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

因此看起来两个对象都存在并且被实例化。但我仍然在下一行(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)

我很困惑。我遗失的哪个对象在这里为空?

1 个答案:

答案 0 :(得分:0)

bindRestaurant的restaurant参数为null。传递给构造函数的餐馆数组似乎有可能是可以为空的元素,如果它甚至已经正确初始化了。

分配数组只是为对象引用创建了一个持有者,你仍然需要在数组的每个位置实际放置一些东西,所以数组都是空的或特定的位置。