spotify-web-api-java: Question Regarding NullPointerException in GetListOfCurrentUsersPlaylistsRequest

Hi,

This is more of a question than a bug report, but I’ve noticed for a while now that sometimes after running the application for a while (1 week+) the GetListOfCurrentUsersPlaylistsRequest starts throwing a NullPointerException consistently for every single request. Restarting the application fixes the issue immediately. I’ve actually first run into this issue quite a while back, probably more than a year, and granted, the production version of the application still uses an old version of the API wrapper (5.0.4) (the update to v6+ caused quite a big change in the code base that was implemented in the development branch of the application, which is not ready for release) but sometimes the issue does not arise for a while. The exception is caused by AbstractRequest#getJson returning null, throwing the NPE when trying to call String#length in StringReader#<init>:

Caused by: java.lang.NullPointerException
	at java.io.StringReader.<init>(StringReader.java:50)
	at com.google.gson.JsonParser.parseString(JsonParser.java:47)
	at com.wrapper.spotify.model_objects.AbstractModelObject$JsonUtil.createModelObjectPaging(AbstractModelObject.java:149)
	at com.wrapper.spotify.requests.data.playlists.GetListOfCurrentUsersPlaylistsRequest.execute(GetListOfCurrentUsersPlaylistsRequest.java:39)

However, the reason why AbstractRequest#getJson returns null seems to be that the endpoint returns an invalid response, either null or an empty string:

  public String getJson() throws
    IOException,
    SpotifyWebApiException,
    ParseException {
    String json = httpManager.get(uri, headers.toArray(new Header[0]));

    if (json == null || json.equals("")) {
      return null;
    } else {
      return json;
    }
  }

So my main question is: does this even have anything to do with the API wrapper or is it an issue with the API? And: is this by chance a known issue that has already been fixed? The fact that rebooting the application fixes the issue seems to indicate that it is the wrapper that’s causing the error, but the fact that the wrapper receives that response seems to indicate that it is something to to with the API, unless it somehow has something to do with the headers the wrapper provides.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 20 (9 by maintainers)

Most upvoted comments

It looks like the beviour is caused by the endpoint responding with a 431

[DEBUG] 2021-04-14 01:12:24.225 [botify command execution: CommandContext@cf09bd99-f564-4962-a65c-ed1348542b9b] SpotifyService - Executing GetListOfCurrentUsersPlaylistsRequest with params: token ...; offset 0; limit 50; uri https://api.spotify.com:443/v1/me/playlists?offset=0&limit=50, headers [Authorization: Bearer ...], body null
Apr 14, 2021 1:12:24 AM com.wrapper.spotify.SpotifyHttpManager execute
CONFIG: The response came from an upstream server
Apr 14, 2021 1:12:24 AM com.wrapper.spotify.SpotifyHttpManager getResponseBody
FINE: The http response has body 
Apr 14, 2021 1:12:24 AM com.wrapper.spotify.SpotifyHttpManager getResponseBody
FINE: The http response has status code 431
Apr 14, 2021 1:12:24 AM com.wrapper.spotify.requests.AbstractRequest returnJson
FINE: The httpManager returned json == "".
[ERROR] 2021-04-14 01:12:24.264 [botify command execution: CommandContext@cf09bd99-f564-4962-a65c-ed1348542b9b] CommandManager - Exception while handling command .search -spotify -own -list songs on guild botify
net.robinfriedli.botify.exceptions.CommandRuntimeException: java.lang.NullPointerException
        at net.robinfriedli.botify.command.interceptor.interceptors.CommandExecutionInterceptor.intercept(CommandExecutionInterceptor.java:125) ~[botify-1.0-SNAPSHOT.jar:?]
        at net.robinfriedli.botify.command.interceptor.AbstractChainableCommandInterceptor.intercept(AbstractChainableCommandInterceptor.java:36) ~[botify-1.0-SNAPSHOT.jar:?]
        at net.robinfriedli.botify.command.interceptor.AbstractChainableCommandInterceptor.intercept(AbstractChainableCommandInterceptor.java:36) ~[botify-1.0-SNAPSHOT.jar:?]
        at net.robinfriedli.botify.command.interceptor.AbstractChainableCommandInterceptor.intercept(AbstractChainableCommandInterceptor.java:36) ~[botify-1.0-SNAPSHOT.jar:?]
        at net.robinfriedli.botify.command.interceptor.AbstractChainableCommandInterceptor.intercept(AbstractChainableCommandInterceptor.java:36) ~[botify-1.0-SNAPSHOT.jar:?]
        at net.robinfriedli.botify.command.interceptor.AbstractChainableCommandInterceptor.intercept(AbstractChainableCommandInterceptor.java:36) ~[botify-1.0-SNAPSHOT.jar:?]
        at net.robinfriedli.botify.command.interceptor.CommandInterceptorChain.intercept(CommandInterceptorChain.java:69) ~[botify-1.0-SNAPSHOT.jar:?]
        at net.robinfriedli.botify.command.CommandManager.lambda$runCommand$0(CommandManager.java:56) ~[botify-1.0-SNAPSHOT.jar:?]
        at java.lang.Thread.run(Thread.java:834) [?:?]
        at net.robinfriedli.botify.concurrent.QueuedThread.run(QueuedThread.java:18) ~[botify-1.0-SNAPSHOT.jar:?]
        at net.robinfriedli.botify.concurrent.CommandExecutionThread.run(CommandExecutionThread.java:23) ~[botify-1.0-SNAPSHOT.jar:?]
Caused by: java.lang.NullPointerException

(token omitted)

Which is strange, because the only header set seems to be the JWT. Pretty sure that’s it though, the log is pretty busy with messages but every failing request logs a 431 and the empty json response.

I’ll temporarily log request parameters before executing a GetListOfCurrentUsersPlaylistsRequest and check what happens if I try executing the request with the same access token from somewhere else when I run into the issue again, it’s a long shot but it helps ruling out any issues with the Spotify API if the endpoint still returns valid JSON responses. Additionally I’ve enabled all logging messages for the com.wrapper.spotify.SpotifyApi logger, I noticed that the SpotifyHttpManager logs whether the response was fetched from the cache and I suspect it might be a caching issue.