Search code examples
javaandroidretrofit2pojo

Retrofit 2 request returns null object reference


Android newbie here! I'm trying to read a JSON like this:

{  
"channel0":{  
  "song":"Crush",
  "artist":"Jennifer Paige",
  "duration":"180",
  "playedat":"1545065265",
  "image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/8eecc92227fcbb09b43472f000df74e1.png"
},
"channel1":{  
  "song":"Reasons Why",
  "artist":"Brand New Immortals",
  "duration":"180",
  "playedat":"1545065371",
  "image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/c059afda95dd35354af26cf72e5deab4.png"
},
"channel2":{  
  "song":"Dance Me To The End Of Love",
  "artist":"Leonard Cohen",
  "duration":"300",
  "playedat":"1545065181",
  "image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/a368617dc7dc4716a9badb523ff6e7d4.png"
},
"channel3":{  
  "song":"4 Minutes",
  "artist":"Madonna",
  "duration":"180",
  "playedat":"1545065300",
  "image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/1dcefc6496be4155a00f919dcbb54f77.png"
},
"channel4":{  
  "song":"Mothers, Sisters, Daughters",
  "artist":"Voxtrot",
  "duration":"180",
  "playedat":"1545065257",
  "image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/5861b97231bd4ffe9218dfcacc27d68a.png"
}
}

from an URL with using Retrofit 2. I'm using this structure in my MainActivity:

retrofit2.Call<List<Channel>> call = restInterface.getChannels();

call.enqueue(new Callback<List<Channel>>() {
    @Override
    public void onResponse(retrofit2.Call<List<Channel>> call, Response<List<Channel>> response) {
        List<Channel> channelList= new ArrayList<>();
        channelList=response.body();
        for (int i=0;i<4;i++){
            System.out.println(""+channelList.get(i).getArtist()+"\n");
        }
    }

and when I run my application, I get this error:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.korhan.frontend, PID: 30563
    java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
        at com.example.korhan.frontend.MainActivity$1.onResponse(MainActivity.java:44)
        at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

By the way I'm using only one Channel class for all of this Channels in the JSON but when I tried to use POJO creator it created 4 classes like Channel1, Channel2... for every Channel in JSON class. Here is my Channel class:

public class Channel {

    @SerializedName("song")
    @Expose
    private String song;
    @SerializedName("artist")
    @Expose
    private String artist;
    @SerializedName("duration")
    @Expose
    private String duration;
    @SerializedName("playedat")
    @Expose
    private String playedAt;
    @SerializedName("image_extralarge")
    @Expose
    private String img;
    //Getters, setters etc.
}

So, how should I parse this JSON in my situation?


Solution

  • your problem is in your json, it should look like:

    [  
     {  
         "song":"Crush",
         "artist":"Jennifer Paige",
         "duration":"180",
         "playedat":"1545065265",
         "image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/8eecc92227fcbb09b43472f000df74e1.png"
     },
     {  
         "song":"Reasons Why",
         "artist":"Brand New Immortals",
         "duration":"180",
         "playedat":"1545065371",
         "image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/c059afda95dd35354af26cf72e5deab4.png"
     },
     {  
         "song":"Dance Me To The End Of Love",
         "artist":"Leonard Cohen",
         "duration":"300",
         "playedat":"1545065181",
         "image_extralarge":"https:\/\/lastfm-img2.akamaized.net\/i\/u\/300x300\/a368617dc7dc4716a9badb523ff6e7d4.png"
     }
    ]
    

    Because the json you have is not considered as List, it's json object that has multiple json objects each with a key, which can be read as Map<String, Object> not a list.