0

I am trying to Access a Json object nested within another Json object. I have written a POJO class but does not allow me to access data:

Here is Json Example:

   {
            "relationships": {
                "competition": {
                    "data": {
                        "type": "competitions",
                        "id": "37"
                    }
                }
            },
            "attributes": {
                "has_league_table": false,
                "name": "Round of 16",
                "stage": "Round of 16"
            },
            "type": "rounds",
            "id": "881"
        },

I want to access the data JSON Object within Relationships->Competitions JSON objects

Here is my POJO Classes:

public class Relationships {

    private Competition competition;

    public Competition getCompetition ()
    {
        return competition;
    }

    public void setCompetition (Competition competition)
    {
        this.competition = competition;
    }


}

Competition POJO

public class Competition {

    private Data data;

    public Data getData ()
    {
        return this.data;
    }

    public void setData (Data data)
    {
        this.data = data;
    }

}

DATA POJO

public class Data {

    private String id;

    private String type;

    public String getId ()
    {
        return this.id;
    }

    public void setId (String id)
    {
        this.id = id;
    }

    public String getType ()
    {
        return this.type;
    }

    public void setType (String type)
    {
        this.type = type;
    }

}

And my Adapter

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

    private List<Included> includedData;


    public Adapter(List<Included> includedData) {
        this.includedData = includedData;

    }


    @Override
    public Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int i) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
        return new ViewHolder(view);
    }

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


        holder.tvCompetitionName.setText(includedData.get(position).getAttributes().getName());
        holder.homeTeam.setText(includedData.get(position).getRelationships().getCompetition().getData().getType());



        holder.itemView.setTag(includedData.get(position));

    }

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

    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tvCompetitionName;
        private TextView homeTeam;
        private TextView score;

        public ViewHolder(View itemView) {
            super(itemView);

            tvCompetitionName = (TextView) itemView.findViewById(R.id.competitionNameTV);
            homeTeam = (TextView) itemView.findViewById(R.id.homeTeamTv);
            score = (TextView) itemView.findViewById(R.id.score);
        }
    }
}

However in my onBindViewHolder

holder.homeTeam.setText(includedData.get(position).getRelationships().getCompetition().getData().getType());

produces error:

java.lang.NullPointerException: Attempt to invoke virtual method 'com.xxx.xxxx.DataModel_Included.Data com.xxxx.xxxx.DataModel_Included.Competition.getData()' on a null object reference

Any help on this?

BigC3886
  • 13
  • 5
  • suppose you can find a solution from this [post](http://stackoverflow.com/questions/22215794/format-of-pojo-for-nested-json) using `Gson` . – Rajith Pemabandu Apr 28 '17 at 00:09
  • I don't see any code which performs deserialization. Basically, you need some component which will create Java objects out of the JSON representations. As @RajithPemabandu points out, Gson (or alternately Jackson) are known tools for this in Java. – Jameson Apr 28 '17 at 00:10

0 Answers0