0

Here I am reading json value from youtube to java. I am getting values properly except the thumbnail data while getting thumbnail object value i am getting java.lang.NullPointerException

public class JsonVideoDetais {

    public static void main(String... args) {
        BufferedReader reader = null;
        StringBuilder buffer = null;
        try {
            String link = "https://gdata.youtube.com/feeds/api/videos/" + "aa_wFClyiVE" + "?v=2&alt=jsonc";
            URL url = new URL(link);
            reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
            buffer = new StringBuilder();
            int read;
            char[] chars = new char[1024];
            while ((read = reader.read(chars)) != -1) {
                buffer.append(chars, 0, read);
            }
        } catch (Exception e) {

        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException ex) {
                    Logger.getLogger(JsonVideoDetais.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        videoDetails data;
        data = new Gson().fromJson(buffer.toString(), videoDetails.class);
        System.out.println(data.getData().getTitle());
        System.out.println(data.getData().getTn().getHqDefault());
        System.out.println(data.getData().getTn().getSqDefault());
    }
    }


class videoDetails {

    private Data data;

    public Data getData() {
        return data;
    }

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

    public String toString() {
        return String.format("data:%s", data);
    }
     }



class Data {

    private String id;
    private String title;
    private String description;
    private int duration;
    private Thumbnail tn;

    public Thumbnail getTn() {
        return tn;
    }

    public void setTn(Thumbnail tn) {
        this.tn = tn;
    }

    public int getDuration() {
        return duration;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

    public String getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String toString() {
        return String.format("title:%s,id:%s,description:%s,tn:%s,duration:%d", title, id, description, tn, duration);
    }
}



 class Thumbnail {

    private String sqDefault;
    private String hqDefault;

    public String getSqDefault() {
        return sqDefault;
    }

    public void setSqDefault(String sqDefault) {
        this.sqDefault = sqDefault;
    }

    public String getHqDefault() {
        return hqDefault;
    }

    public void setHqDefault(String hqDefault) {
        this.hqDefault = hqDefault;
    }

    public String toString() {
        return String.format("sqDefault:%s,hqDefault:%s", hqDefault, sqDefault);
    }
}

I am getting following exception

Exception in thread "main" java.lang.NullPointerException
    at utility.JsonVideoDetais.main(JsonVideoDetais.java:52)

while calling

 System.out.println(data.getData().getTn().getHqDefault());
            System.out.println(data.getData().getTn().getSqDefault());

If you wll see this link. It is having value for sqDefault and hqDefault

I would like to fetch the value of sqDefault and hqDefault. How to do this.

6
  • 1
    And which line is 52? Commented Jan 15, 2014 at 9:51
  • System.out.println(data.getData().getTn().getHqDefault()); Commented Jan 15, 2014 at 9:59
  • Something is null. Split that chained call out into separate lines with temporary variables, and see which one is null. It really looks like getTn() is returning null. Commented Jan 15, 2014 at 10:05
  • Yes getTn() is null. Why it is not loading the value Commented Jan 15, 2014 at 10:07
  • Is your connection using a proxy? Check if any exception has been thrown while your are trying to connect, ignore it isn't a good idea. Commented Jan 15, 2014 at 10:30

2 Answers 2

2

In your Data class, i created an object like this. I guess the Thumbnail object is getting set to thumbnail, tn is not working on my side too.

private Thumbnail thumbnail;// instead of tn

and the resultant output is : -

Blood Glucose Hindi - Dr. Anup, MD Teaches Series
https://i1.ytimg.com/vi/aa_wFClyiVE/hqdefault.jpg
https://i1.ytimg.com/vi/aa_wFClyiVE/default.jpg
Sign up to request clarification or add additional context in comments.

1 Comment

"tn" is not present in your gdata as you can see and mentioned earlier by Weibo Li, but thumbnail you can see. This resembles to POJO of tables in JPA or hibernate tools, the same column name should be used for varibale name in your POJO. similarly here also.
1

Using debugger to find out which object is null is fastest way to solve your problem.

OR

Find the null return value with the following code:

System.out.println(data);
System.out.println(data.getData());
System.out.println(data.getData().getTn());

--The following text are newly added-----------------

Well, I have run your program on my laptop, and it seem that the json response of https://gdata.youtube.com/feeds/api/videos/aa_wFClyiVE?v=2&alt=jsonc#data/thumbnail/hqDefault contains no tn field at all. That's why you always got null value.

6 Comments

Yes, and this should really be a comment.
I have seen value is not null there
That can not be possible. Try step into every get method, and watch which variable turns out to be null. @manish Sahu
I've already modified my answer. Check it out. Maybe it can help. @manish Sahu
getTn() is null but if you will see link Its value is not null i want to fetch those values
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.