gpt4 book ai didi

java - 我如何知道是什么导致我的实体类 getTeamId 中出现空指针

转载 作者:行者123 更新时间:2023-12-01 17:20:06 25 4
gpt4 key购买 nike

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

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

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

在我的适配器类中,我有以下内容,它说 team 为空,我需要向其中添加一些内容吗?

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)

最佳答案

您要传递给 teamEntity 的内容是什么?

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

关于java - 我如何知道是什么导致我的实体类 getTeamId 中出现空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61311194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com