I have tried to understand what is causing my getTeamId() method in my entity return null, but I can't seem to crack the code. I have attached my adapter class, entity class and the exception thrown.
Entity Class TeamEntity
@SerializedName("idTeam")
private String teamId;
public String getTeamId() {
return teamId;
}
public void setTeamId(String teamId) {
this.teamId = teamId;
}
And in my adapter class I have the following, it says team is null do i need to add something to it?
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);
}
// Stack Trace
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)