我怎么知道是什么导致我的实体类getTeamId中的空指针

问题描述 投票:0回答:1

我试图理解是什么导致我的实体中的getTeamId()方法返回null,但是我似乎无法破解代码。我已附加了适配器类,实体类和引发的异常。

    Entity Class TeamEntity
 @SerializedName("idTeam")
    private String teamId;
        public String getTeamId() {
            return teamId;
        }

        public void setTeamId(String teamId) {
            this.teamId = teamId;
    }

并且在我的适配器类中,我有以下内容,它说team为null,我需要添加一些东西吗?

public class MatchAdapter extends RecyclerView.Adapter<MatchAdapter.ViewHolder> {
    private Context context;
    private TeamEntity team;
    private List<MatchEntity> matchList;

    public MatchAdapter(Context context, TeamEntity teamEntity) {
        this.context = context;
        this.team = teamEntity;
        matchList = new ArrayList<>();
    }

    public void setList(List<MatchEntity> matchList) {
        this.matchList.addAll(matchList);
        Collections.sort(this.matchList); //date order
        notifyDataSetChanged();
    }

    public void updateMatch(MatchEntity match) {
        matchList.set(matchList.indexOf(match), match);
        notifyItemChanged(matchList.indexOf(match));
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        MatchEntity match = matchList.get(position);
        holder.bind(match);
    }

    @Override
    public int getItemCount() {
        return matchList.size();
    }
    @Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    MatchEntity match = matchList.get(position);
    holder.bind(match);
}

     void bind(MatchEntity match) {

                String homeTeam = match.getHomeTeam() != null ? match.getHomeTeam() : "";


                if (team.getTeamId().equals(match.getIdGuestTeam())) {
                    homeTextView.setText(homeTeam);


                } else {
                    homeTextView.setText(guestTeam);

                }

//堆栈跟踪

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.sportsapp.data.room.entities.TeamEntity.getTeamId()' on a null object reference
        at com.sportsapp.presentation.ui.adapters.MatchAdapter$ViewHolder.bind(MatchAdapter.java:118)
        at com.sportsapp.presentation.ui.adapters.MatchAdapter.onBindViewHolder(MatchAdapter.java:59)
        at com.sportsapp.presentation.ui.adapters.MatchAdapter.onBindViewHolder(MatchAdapter.java:27)
java android nullpointerexception null
1个回答
0
投票

您要传递给teamEntity的什么?

public MatchAdapter(Context context, TeamEntity teamEntity) {
        this.context = context;
        this.team = teamEntity;
        matchList = new ArrayList<>();
    }
© www.soinside.com 2019 - 2024. All rights reserved.