gpt4 book ai didi

从 json 读取值到 java 对象时出现 java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-12-01 23:16:26 24 4
gpt4 key购买 nike

这里我正在从 youtube 读取 json 值到 java。我在获取缩略图对象值时正确获取了除缩略图数据之外的值,我得到了 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);
}
}

我遇到以下异常

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

通话时

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

如果你会看到这个 link 。它对 sqDefaulthqDefault

有值(value)

我想获取 sqDefaulthqDefault 的值。如何做到这一点。

最佳答案

在您的数据类中,我创建了一个像这样的对象。我猜想缩略图对象正在设置为缩略图,tn 在我这边也不起作用。

private Thumbnail thumbnail;// instead of tn

结果输出是: -

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

关于从 json 读取值到 java 对象时出现 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21133962/

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