[play 2.4 - java] Execution exception [NullPointerException: scheme]

2,635 views
Skip to first unread message

kewalgr...@gmail.com

unread,
Jun 18, 2015, 7:09:34 AM6/18/15
to play-fr...@googlegroups.com

I am trying to write a simple application in Play2.4. I want to get a page and display it on my localhost which will act as a proxy. Below is my action in the controller:

public class Proxy extends Controller{
    @Inject WSClient ws;

    public Promise<Result> index(String url){
        WSRequest request = ws.url(url);
        //Promise<WSResponse> responsePromise = request.get();
         return request.get().map((r) -> ok(r.getBody()).as("text/html"));
} }

I basically want the above task to be done in async. Thanks

PS: I have been following this link https://www.playframework.com/documentation/2.4.x/JavaAsyncand https://www.playframework.com/documentation/2.3.x/JavaAsync.

I have tried going through the docs and other sources as well bu to no avail. Any help would be appreciated. Thanks

Unable to get any answers...went through the stack trace but could not get anything... Below is the stack trace if someone is interested.

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[NullPointerException: scheme]]
    at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:254) ~[play_2.11-2.4.0.jar:2.4.0]
    at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:180) ~[play_2.11-2.4.0.jar:2.4.0]
    at play.api.GlobalSettings$class.onError(GlobalSettings.scala:179) [play_2.11-2.4.0.jar:2.4.0]
    at play.api.DefaultGlobal$.onError(GlobalSettings.scala:212) [play_2.11-2.4.0.jar:2.4.0]
    at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:94) [play_2.11-2.4.0.jar:2.4.0]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$9$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:158) [play-netty-server_2.11-2.4.0.jar:2.4.0]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$9$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:155) [play-netty-server_2.11-2.4.0.jar:2.4.0]
    at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36) [scala-library-2.11.6.jar:na]
    at scala.util.Failure$$anonfun$recover$1.apply(Try.scala:215) [scala-library-2.11.6.jar:na]
    at scala.util.Try$.apply(Try.scala:191) [scala-library-2.11.6.jar:na]
    at scala.util.Failure.recover(Try.scala:215) [scala-library-2.11.6.jar:na]
    at scala.concurrent.Future$$anonfun$recover$1.apply(Future.scala:324) [scala-library-2.11.6.jar:na]
    at scala.concurrent.Future$$anonfun$recover$1.apply(Future.scala:324) [scala-library-2.11.6.jar:na]
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32) [scala-library-2.11.6.jar:na]
    at play.api.libs.iteratee.Execution$trampoline$.executeScheduled(Execution.scala:109) [play-iteratees_2.11-2.4.0.jar:2.4.0]
    at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:71) [play-iteratees_2.11-2.4.0.jar:2.4.0]
    at scala.concurrent.impl.CallbackRunnable.executeWithValue(Promise.scala:40) [scala-library-2.11.6.jar:na]
    at scala.concurrent.impl.Promise$DefaultPromise.tryComplete(Promise.scala:248) [scala-library-2.11.6.jar:na]
    at scala.concurrent.Promise$class.complete(Promise.scala:55) [scala-library-2.11.6.jar:na]
    at scala.concurrent.impl.Promise$DefaultPromise.complete(Promise.scala:153) [scala-library-2.11.6.jar:na]
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:23) [scala-library-2.11.6.jar:na]
    at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:40) [akka-actor_2.11-2.3.11.jar:na]
    at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:397) [akka-actor_2.11-2.3.11.jar:na]
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) [scala-library-2.11.6.jar:na]
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) [scala-library-2.11.6.jar:na]
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) [scala-library-2.11.6.jar:na]
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) [scala-library-2.11.6.jar:na]
Caused by: java.lang.NullPointerException: scheme
    at com.ning.http.client.uri.Uri.<init>(Uri.java:56) ~[async-http-client-1.9.21.jar:na]
    at com.ning.http.client.uri.Uri.create(Uri.java:32) ~[async-http-client-1.9.21.jar:na]
    at com.ning.http.client.uri.Uri.create(Uri.java:25) ~[async-http-client-1.9.21.jar:na]
    at com.ning.http.client.RequestBuilderBase.setUrl(RequestBuilderBase.java:307) ~[async-http-client-1.9.21.jar:na]
    at com.ning.http.client.RequestBuilder.setUrl(RequestBuilder.java:165) ~[async-http-client-1.9.21.jar:na]
    at play.libs.ws.ning.NingWSRequest.buildRequest(NingWSRequest.java:401) ~[play-java-ws_2.11-2.4.0.jar:2.4.0]
    at play.libs.ws.ning.NingWSRequest.execute(NingWSRequest.java:394) ~[play-java-ws_2.11-2.4.0.jar:2.4.0]
    at play.libs.ws.ning.NingWSRequest.execute(NingWSRequest.java:389) ~[play-java-ws_2.11-2.4.0.jar:2.4.0]
    at play.libs.ws.ning.NingWSRequest.get(NingWSRequest.java:272) ~[play-java-ws_2.11-2.4.0.jar:2.4.0]
    at controllers.Proxy.index(Proxy.java:46) ~[classes/:2.4.0]
    at router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$4$$anonfun$apply$4.apply(Routes.scala:157) ~[classes/:na]
    at router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$4$$anonfun$apply$4.apply(Routes.scala:157) ~[classes/:na]
    at play.core.routing.HandlerInvokerFactory$$anon$5.resultCall(HandlerInvoker.scala:139) ~[play_2.11-2.4.0.jar:2.4.0]
    at play.core.routing.HandlerInvokerFactory$JavaActionInvokerFactory$$anon$14$$anon$3$$anon$1.invocation(HandlerInvoker.scala:127) ~[play_2.11-2.4.0.jar:2.4.0]
    at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:65) ~[play_2.11-2.4.0.jar:2.4.0]
    at play.http.DefaultHttpRequestHandler$1.call(DefaultHttpRequestHandler.java:20) ~[play_2.11-2.4.0.jar:2.4.0]
    at play.core.j.JavaAction$$anonfun$7.apply(JavaAction.scala:85) ~[play_2.11-2.4.0.jar:2.4.0]
    at play.core.j.JavaAction$$anonfun$7.apply(JavaAction.scala:85) ~[play_2.11-2.4.0.jar:2.4.0]
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24) [scala-library-2.11.6.jar:na]
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) [scala-library-2.11.6.jar:na]
    at play.core.j.HttpExecutionContext$$anon$2.run(HttpExecutionContext.scala:40) ~[play_2.11-2.4.0.jar:2.4.0]
    at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:70) [play-iteratees_2.11-2.4.0.jar:2.4.0]
    at play.core.j.HttpExecutionContext.execute(HttpExecutionContext.scala:32) ~[play_2.11-2.4.0.jar:2.4.0]
    at scala.concurrent.impl.Future$.apply(Future.scala:31) ~[scala-library-2.11.6.jar:na]
    at scala.concurrent.Future$.apply(Future.scala:492) ~[scala-library-2.11.6.jar:na]
    at play.core.j.JavaAction.apply(JavaAction.scala:85) ~[play_2.11-2.4.0.jar:2.4.0]
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4$$anonfun$apply$5.apply(Action.scala:105) ~[play_2.11-2.4.0.jar:2.4.0]
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4$$anonfun$apply$5.apply(Action.scala:105) ~[play_2.11-2.4.0.jar:2.4.0]
    at play.utils.Threads$.withContextClassLoader(Threads.scala:21) ~[play_2.11-2.4.0.jar:2.4.0]
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4.apply(Action.scala:104) ~[play_2.11-2.4.0.jar:2.4.0]
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4.apply(Action.scala:103) ~[play_2.11-2.4.0.jar:2.4.0]
    at scala.Option.map(Option.scala:146) ~[scala-library-2.11.6.jar:na]
    at play.api.mvc.Action$$anonfun$apply$1.apply(Action.scala:103) ~[play_2.11-2.4.0.jar:2.4.0]
    at play.api.mvc.Action$$anonfun$apply$1.apply(Action.scala:96) ~[play_2.11-2.4.0.jar:2.4.0]
    at play.api.libs.iteratee.Iteratee$$anonfun$mapM$1.apply(Iteratee.scala:524) ~[play-iteratees_2.11-2.4.0.jar:2.4.0]
    at play.api.libs.iteratee.Iteratee$$anonfun$mapM$1.apply(Iteratee.scala:524) ~[play-iteratees_2.11-2.4.0.jar:2.4.0]
    at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM$1.apply(Iteratee.scala:560) ~[play-iteratees_2.11-2.4.0.jar:2.4.0]
    at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM$1.apply(Iteratee.scala:560) ~[play-iteratees_2.11-2.4.0.jar:2.4.0]
    at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$1$$anonfun$apply$13.apply(Iteratee.scala:536) ~[play-iteratees_2.11-2.4.0.jar:2.4.0]
    at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$1$$anonfun$apply$13.apply(Iteratee.scala:536) ~[play-iteratees_2.11-2.4.0.jar:2.4.0]
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24) [scala-library-2.11.6.jar:na]
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) [scala-library-2.11.6.jar:na]
    ... 6 common frames omitted
The related stack overflow question can be found here http://stackoverflow.com/questions/30891813/null-pointer-exceptionnot-solved-and-unable-to-map-wsresponse-to-result-in-pla
Also, this is my first time posting a question here. If this is not the correct place or if i have missed something, i humbky request to forgive me and guide me to the appropriate ones. Thanks

Mariot Chauvin

unread,
Jun 18, 2015, 7:46:18 AM6/18/15
to play-fr...@googlegroups.com
The NPE is caused because the scheme of you URL is not defined.
Check you have a valid URL before calling ws.url(url)

--
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/b8062704-6d92-4914-aade-1dc7648ab43d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



Visit theguardian.com. On your mobile and tablet, download the Guardian iPhone and Android apps theguardian.com/guardianapp and our tablet editions theguardian.com/editions.  Save up to 57% by subscribing to the Guardian and Observer - choose the papers you want and get full digital access.  Visit subscribe.theguardian.com

This e-mail and all attachments are confidential and may also be privileged. If you are not the named recipient, please notify the sender and delete the e-mail and all attachments immediately. Do not disclose the contents to another person. You may not use the information for any purpose, or store, or copy, it in any way.  Guardian News & Media Limited is not liable for any computer viruses or other material transmitted with or as part of this e-mail. You should employ virus checking software.
 
Guardian News & Media Limited is a member of Guardian Media Group plc. Registered Office: PO Box 68164, Kings Place, 90 York Way, London, N1P 2AP.  Registered in England Number 908396


Mahendra Shinde

unread,
Jun 22, 2016, 10:44:53 AM6/22/16
to play-framework
Hi all,

I am same example with 2.5.4 but still null pointer exception in ws.url(). I followed migration document https://www.playframework.com/documentation/2.5.x/JavaMigration25#Replaced-F.Promise-with-Java-8s-CompletionStage

I have play 2.5.4 and Scala 2.11.7 
Below is code snippet from testing.

    import javax.inject.Inject;
    import play.mvc.*;
    import play.libs.ws.*;
    import java.util.concurrent.*;
    import org.w3c.dom.Document;

public class WSApplication extends Controller {
            static String url = "http://www.google.com";
            @Inject 
            static  WSClient ws;
public static CompletionStage<Result> index(){
        WSRequest request = ws.url(url); //exception here

        return request.get().thenApplyAsync((r) -> ok(r.getBody()).as("text/html"));
}

Kindly direct me.

Thank you.
Mahendra 

Igmar Palsenberg

unread,
Jun 22, 2016, 12:17:48 PM6/22/16
to play-framework


Op woensdag 22 juni 2016 16:44:53 UTC+2 schreef Mahendra Shinde:
Hi all,

I am same example with 2.5.4 but still null pointer exception in ws.url(). I followed migration document https://www.playframework.com/documentation/2.5.x/JavaMigration25#Replaced-F.Promise-with-Java-8s-CompletionStage

I have play 2.5.4 and Scala 2.11.7 
Below is code snippet from testing.

    import javax.inject.Inject;
    import play.mvc.*;
    import play.libs.ws.*;
    import java.util.concurrent.*;
    import org.w3c.dom.Document;

public class WSApplication extends Controller {
            static String url = "http://www.google.com";
            @Inject 
            static  WSClient ws;
public static CompletionStage<Result> index(){
        WSRequest request = ws.url(url); //exception here

        return request.get().thenApplyAsync((r) -> ok(r.getBody()).as("text/html"));
}


Guice doesn't inject static members. 


Igmar 

Mahendra Shinde

unread,
Jun 22, 2016, 1:15:54 PM6/22/16
to play-framework
Thank you for replay

But I tried both static and non static ways. Also I wrote same code in Scala there is also NullPointerException 

Scala:
class App {
@Inject var ws: WSClient=null
def non_static = 
{ws.url("http://google.com").get()
}
}

 object ScalaApplication extends Controller {
  def index = Action.async {
    new App().non_static.map(r => Ok(r.body))
  }

}


Java:
public class WSApplication extends Controller {
String url = "http://www.google.com";
@Inject WSClient ws;
public CompletionStage<Result> non_static_demo(){
WSRequest request = ws.url(url);
return request.get().thenApplyAsync((r) -> ok(r.getBody()).as("text/html"));
}
public static CompletionStage<Result> index(){
return new WSApplication().non_static_demo();
}
}

Justin Nichols

unread,
Jun 22, 2016, 1:23:01 PM6/22/16
to play-framework
What do you have set in build.sbt for the configuration key:  routesGenerator

To enable dynamic injection I believe it must be:

routesGenerator := InjectedRoutesGenerator

Hope it helps some!

Thanks,

Justin




--
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.

Greg Methvin

unread,
Jun 22, 2016, 1:54:24 PM6/22/16
to play-framework
Hi Mahendra,

The constructor new App() will not actually inject any of the members of your class. The class needs to be created by the injector for the fields to be set properly. I would suggest the following:

class MainController @Inject() (ws: WSAPI) extends Controller {
  val url = "http://www.google.com
  def index = Action.async {
    ws.url(url).get().map(r => Ok(r.body))
  }
}

and in build.sbt:

routesGenerator := InjectedRoutesGenerator

It would work just as well to inject a field of type WSAPI, but I recommend constructor injection because it makes it impossible to create the class without passing the dependencies. It's just a question of whether you do it manually (e.g. for a simple unit test) or have it done automatically by the framework.

Everything should be a class. There should be no need to use statics at all in your controllers (except for constants), and doing so is going to make it harder to use dependency injection properly.

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.

For more options, visit https://groups.google.com/d/optout.



--
Greg Methvin
Senior Software Engineer

Mahendra Shinde

unread,
Jun 22, 2016, 4:48:42 PM6/22/16
to play-framework
Hi Greg, Justin 
Thank you Justin

Thank you Greg.


I made changes in build.sbt and also tried MainController 
But now I am not able to access index() directly via routes. As it is no longer static.   

All above code was handwritten as case was small. Is there any tool or framework to create code? Its following design patterns.


Thank you,
Mahendra 

Greg Methvin

unread,
Jun 22, 2016, 4:58:57 PM6/22/16
to play-framework
Hi Mahendra,

Simply writing controllers.MainController.index() in your routes should work fine. The injected routes generator will assume index is a method of the class as opposed to a static method.

Greg


For more options, visit https://groups.google.com/d/optout.

Mahendra Shinde

unread,
Jun 22, 2016, 5:30:42 PM6/22/16
to play-fr...@googlegroups.com
Thank you Greg 

Its working. You made my 1 week of holidays productive. 

Thank you,
Mahendra 


 

--
You received this message because you are subscribed to a topic in the Google Groups "play-framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/play-framework/RI7GuL5NcDA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/CAA%3D11Hxx_2g--Dsgv0tNa9fjKCFbUR_-fQDcv-i6%2Bc9%3DA%2BjZjQ%40mail.gmail.com.

Igmar Palsenberg

unread,
Jun 23, 2016, 2:19:33 AM6/23/16
to play-framework
 
Java:
public class WSApplication extends Controller {
String url = "http://www.google.com";
@Inject WSClient ws;
public CompletionStage<Result> non_static_demo(){
WSRequest request = ws.url(url);
return request.get().thenApplyAsync((r) -> ok(r.getBody()).as("text/html"));
}
public static CompletionStage<Result> index(){
return new WSApplication().non_static_demo();
}
}

new ....() bypasses dependency injection. You must get the object from the Injector for this to work.


Igmar 
Reply all
Reply to author
Forward
0 new messages