属性返回Null指针异常

时间:2019-12-19 07:52:56

标签: java android android-recyclerview nullpointerexception

NULL POINTER EXCEPTION HAPPENDS HERE我有2个recyclerviews和2个适配器。一种用于商店名称,一种用于每个商店的可交付成果。我需要显示每个商店的可交付成果。当我填充布局组件时,可交付的imageview返回null。

这是我的代码,用于填充可交付成果的回收站视图

public class DeliverableView extends ConstraintLayout {

    @BindView(R.id.iv_deliverable_image)
    ImageView iv_deliverable_image;
    @BindView(R.id.tv_deliverable_name)
    TextView tv_deliverable_name;
    @BindView(R.id.tv_deliverable_size)
    TextView tv_deliverable_size;
    @BindView(R.id.tv_deliverable_quantity)
    TextView tv_deliverable_quantity;
    @BindView(R.id.tv_deliverable_amount)
    TextView tv_deliverable_amount;

    public DeliverableView(Context context) {
        super(context);
    }

    public DeliverableView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DeliverableView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void populate(Entity deliverable) {
        Glide.with(getContext()).load(StringUtil.trimUrlByHeight(deliverable.attributes.image_url, 60)).into(iv_deliverable_image);
        tv_deliverable_quantity.setText(String.valueOf(deliverable.attributes.count));
        tv_deliverable_name.setText(deliverable.attributes.name);
        tv_deliverable_size.setText(deliverable.attributes.size);
        tv_deliverable_amount.setText(StringUtil.getDisplayedBill(deliverable.attributes.amount_in_cents * deliverable.attributes.count));
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        View.inflate(getContext(), R.layout.layout_componet_deliverable, this);
        ButterKnife.bind(this);
    }
}

Below is the code for the adapter of shops. This is where I setup the deliverableadapter and updating it.

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

    private Context context;
    private LayoutInflater inflater;
    private List<Entity> jobOrders = new ArrayList<>();
    private DeliverableAdapter deliverableAdapter;
    private List<OrderItem> orderItems = new ArrayList<>();v


    public ViewReceiptShopNameAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    public void updateViewShopAdapter(List<Entity> jobOrders) {
        this.jobOrders = jobOrders;
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        return new ViewHolderShopName(inflater.inflate(R.layout.layout_view_receipt_shopname, parent, false));
    }

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

        final Entity jobOrder = jobOrders.get(position);
//        final OrderItem orderItemList = orderItems.get(position);
        ViewHolderShopName viewHolder = (ViewHolderShopName) holder;
        viewHolder.bindModel(jobOrder);
    }

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

    class ViewHolderShopName extends RecyclerView.ViewHolder {

        @BindView(R.id.store_name)
        TextView tv_shop_name;
        @BindView(R.id.count_items_delivered)
        TextView tv_items_count;
        @BindView(R.id.rv_receipt_items)
        RecyclerView rv_receipt_items;

        ViewHolderShopName(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        void bindModel(Entity jobOrder) {
//            tv_shop_name.setText(orderItem.shop.attributes.name);
            tv_items_count.setText(String.format("%d items delivered", jobOrder.attributes.deliverables_purchased_count));

            //deliverable adapter
            LinearLayoutManager layoutManager = new LinearLayoutManager(context);
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            rv_receipt_items.setLayoutManager(layoutManager);
            deliverableAdapter = new DeliverableAdapter(context);
            rv_receipt_items.setAdapter(deliverableAdapter);

            deliverableAdapter.updateDeliverableAdapter(Arrays.asList(jobOrder.relationships.deliverables.data));

        }
    }
}

这是 DeliverableAdapter.class ,我在其中调用updateDeliverableAdapter()方法。

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

    private Context context;
    private LayoutInflater inflater;
    private List<Entity> deliverables = new ArrayList<>();

    public DeliverableAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    public void updateDeliverableAdapter(List<Entity> deliverables) {
        this.deliverables = deliverables;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, final int viewType) {
        return new ViewHolderDeliverable(inflater.inflate(R.layout.layout_item_deliverable, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        final Entity deliverable = deliverables.get(position);
        ViewHolderDeliverable viewHolder = (ViewHolderDeliverable) holder;
        viewHolder.bindModel(deliverable);
    }

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

    class ViewHolderDeliverable extends RecyclerView.ViewHolder {

        @BindView(R.id.deliverableView)
        DeliverableView deliverableView;

        ViewHolderDeliverable(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        void bindModel(Entity deliverable) {
            deliverableView.populate(deliverable);
        }
    }
}

这是我的Entity.class。我检查了AttributesBase是否包含需要的数据。

public class Entity implements BaseModel{

    public String id;
    public String type;
    public AttributesBase attributes;
    public RelationshipsBase relationships;

    public boolean isCancelled(){
        return attributes.status.equalsIgnoreCase("cancelled");
    }

    @Override
    public String toString() {
        return new Gson().toJson(this);
    }

    @Override
    public int hashCode() {
        return toString().hashCode();
    }
}

这些是我正在AttributesBase.class上访问的属性。

  

@SerializedName(“ image-url”)       公共字符串image_url; @SerializedName(“名称”)       公共字符串名称;公共字符串大小;公共整数计数;

1 个答案:

答案 0 :(得分:0)

您正在尝试绑定类Entity中不存在的值。

根据所附的屏幕快照,实体可交付成果仅包含两个成员,即id和type。并且您正在尝试访问可交付对象中不存在的image_url的值。您尝试设置交付品中不存在的所有行时,都会出现此错误。

可交付对象不包含诸如image_url,计数,名称,大小之类的属性。 在所有这些地方,您将获得NullPointException。