Skip to content

Commit 578e0f1

Browse files
authored
fix json null check (#298)
1 parent c2d4284 commit 578e0f1

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

main/src/main/java/com/github/topi314/lavasrc/yandexmusic/YandexMusicSourceManager.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,15 @@ private AudioSearchResult getSearchResult(String query, Set<AudioSearchResult.Ty
100100
+ "&type=all"
101101
+ "&page=0"
102102
);
103+
if (json == null) {
104+
return AudioSearchResult.EMPTY;
105+
}
103106
if (setOfTypes.isEmpty()) {
104107
setOfTypes = SEARCH_TYPES;
105108
}
106109

107110
var resultJson = json.get("result");
108-
if (json.isNull() || resultJson.isNull()) {
111+
if (resultJson.isNull()) {
109112
return AudioSearchResult.EMPTY;
110113
}
111114

@@ -343,7 +346,7 @@ private AudioItem getRecommendations(String identifier) throws IOException {
343346
}
344347

345348
var json = this.getJson(PUBLIC_API_BASE + "/tracks/" + identifier + "/similar");
346-
if (json.isNull() || json.get("result").isNull() || json.get("result").get("similarTracks").values().isEmpty()) {
349+
if (json == null || json.get("result").isNull() || json.get("result").get("similarTracks").values().isEmpty()) {
347350
return AudioReference.NO_TRACK;
348351
}
349352
var tracks = this.parseTracks(json.get("result").get("similarTracks"), "com");
@@ -363,7 +366,7 @@ private AudioItem getRecommendations(String identifier) throws IOException {
363366

364367
private AudioItem getSearch(String query) throws IOException {
365368
var json = this.getJson(PUBLIC_API_BASE + "/search?text=" + URLEncoder.encode(query, StandardCharsets.UTF_8) + "&type=track&page=0");
366-
if (json.isNull() || json.get("result").get("tracks").isNull()) {
369+
if (json == null || json.get("result").get("tracks").isNull()) {
367370
return AudioReference.NO_TRACK;
368371
}
369372
var tracks = this.parseTracks(json.get("result").get("tracks").get("results"), "com");
@@ -375,7 +378,7 @@ private AudioItem getSearch(String query) throws IOException {
375378

376379
private AudioItem getAlbum(String id, String domainEnd) throws IOException {
377380
var json = this.getJson(PUBLIC_API_BASE + "/albums/" + id + "/with-tracks?page-size=" + ALBUM_MAX_PAGE_ITEMS * albumLoadLimit);
378-
if (json.isNull() || json.get("result").isNull()) {
381+
if (json == null || json.get("result").isNull()) {
379382
return AudioReference.NO_TRACK;
380383
}
381384
var tracks = new ArrayList<AudioTrack>();
@@ -404,15 +407,15 @@ private AudioItem getAlbum(String id, String domainEnd) throws IOException {
404407

405408
private AudioItem getTrack(String id, String domainEnd) throws IOException {
406409
var json = this.getJson(PUBLIC_API_BASE + "/tracks/" + id);
407-
if (json.isNull() || json.get("result").values().get(0).get("available").text().equals("false")) {
410+
if (json == null || json.get("result").values().get(0).get("available").text().equals("false")) {
408411
return AudioReference.NO_TRACK;
409412
}
410413
return this.parseTrack(json.get("result").values().get(0), domainEnd);
411414
}
412415

413416
private AudioItem getArtist(String id, String domainEnd) throws IOException {
414417
var json = this.getJson(PUBLIC_API_BASE + "/artists/" + id + "/tracks?page-size=" + ARTIST_MAX_PAGE_ITEMS * artistLoadLimit);
415-
if (json.isNull() || json.get("result").values().isEmpty()) {
418+
if (json == null || json.get("result").values().isEmpty()) {
416419
return AudioReference.NO_TRACK;
417420
}
418421

@@ -422,6 +425,9 @@ private AudioItem getArtist(String id, String domainEnd) throws IOException {
422425
}
423426

424427
var artistJsonResponse = this.getJson(PUBLIC_API_BASE + "/artists/" + id);
428+
if (artistJsonResponse == null) {
429+
return AudioReference.NO_TRACK;
430+
}
425431
var artistJson = artistJsonResponse.get("result").get("artist");
426432
var author = artistJson.get("name").text();
427433

@@ -457,7 +463,7 @@ private AudioItem getPlaylist(String userString, String id, String domainEnd) th
457463
}
458464

459465
private AudioItem getPlaylist(JsonBrowser json, String domainEnd, String playlistUrl) {
460-
if (json.isNull() || json.get("result").isNull() || json.get("result").get("tracks").values().isEmpty()) {
466+
if (json == null || json.get("result").isNull() || json.get("result").get("tracks").values().isEmpty()) {
461467
return AudioReference.NO_TRACK;
462468
}
463469
var tracks = this.parseTracks(json.get("result").get("tracks"), domainEnd);

0 commit comments

Comments
 (0)