WSclient throws NullPointerException

576 views
Skip to first unread message

Jan Ouwens

unread,
May 2, 2016, 6:45:43 AM5/2/16
to play-framework
I'm using Play Framework 2.4.6 with Scala 2.11.8.

The following code throws an NPE:

    val id = ???
    val url = s"/api/customer/$id"
    val request: WSRequest = dependencies.wsClient.url(url)
    val result = request.get()


With the following stack trace:

    java.lang.NullPointerException: scheme
       at com.ning.http.client.uri.Uri.<init>(Uri.java:56)
       at com.ning.http.client.uri.Uri.create(Uri.java:32)
       at com.ning.http.client.uri.Uri.create(Uri.java:25)
       at com.ning.http.client.RequestBuilderBase.setUrl(RequestBuilderBase.java:307)
       at com.ning.http.client.RequestBuilder.setUrl(RequestBuilder.java:165)
       at play.api.libs.ws.ning.NingWSRequest.buildRequest(NingWS.scala:218)
       at play.api.libs.ws.ning.NingWSRequest.execute(NingWS.scala:128)
       at play.api.libs.ws.WSRequest$class.get(WS.scala:408)
       at play.api.libs.ws.ning.NingWSRequest.get(NingWS.scala:81)


It's been a while since I last saw a NullPointerException in Scala ;). However, it took me and my team way too long to figure out that the issue was simply that we neglected to give a well-formed uri. (Should have been something like s"http://localhost:9000/api/customer/$id".) At first, we thought something was wrong with our dependency injection.

I was wondering, is it possible for Play to wrap this NPE and change it into a more helpful error message, like "invalid URI" or something? Maybe wrapped in an exception that isn't a NullPointerException ;) ?

I've asked the maintainer of AsyncHttpClient, but he didn't seem receptive to the idea: https://github.com/AsyncHttpClient/async-http-client/issues/1149

What's your take on this?


Regards,
Jan

Greg Methvin

unread,
May 2, 2016, 7:09:56 AM5/2/16
to play-framework
If we were to fix it in Play, it would be better to validate the URL ahead of time before passing it to RequestBuilder#setUrl. I'm not a fan of catching and re-wrapping exceptions since the API doesn't really give us a guarantee of what the exception will look like.

I agree that the exception is unhelpful, though. It would be nice if AHC did the validation ahead of time and threw a more useful error message, especially since the NPE is being created by internal AHC code.

Greg

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/ae23e25e-11bb-4029-bb57-12f2c1e25c00%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Greg Methvin
Senior Software Engineer

Will Sargent

unread,
May 2, 2016, 4:14:29 PM5/2/16
to play-fr...@googlegroups.com
I'd look into validating the URL with scala-uri or another RFC 3986 compliant URI parser like palominolabs/url-builder.

Will.

Reply all
Reply to author
Forward
0 new messages