ProvisionException using PlayFramework 2.4.2

I am transferring a project from Play 2.2.4 to 2.4.2, and I get an exception that I cannot understand and solve.

Unexpected exception

ProvisionException: Unable to provision, see the following errors:

1) Error injecting constructor, java.lang.NullPointerException
  at controllers.Application.<init>(Application.java:33)
  while locating controllers.Application
    for parameter 1 at router.Routes.<init>(Routes.scala:36)
  while locating router.Routes
  while locating play.api.inject.RoutesProvider
  while locating play.api.routing.Router

1 error

This happens because I added dependency injection for the WS API, for example:

public class Application extends Controller {

    @Inject
    WSClient ws;

    WSRequest request = ws.url("https://...");

    ...
}

The file build.sbtcontains the necessary configuration.

libraryDependencies ++= Seq(
  javaJdbc,
  cache,
  javaWs
)

// Play provides two styles of routers, one expects its actions to be injected, the
// other, legacy style, accesses its actions statically.
routesGenerator := InjectedRoutesGenerator

What could be missing or done wrong?

+4
source share
1 answer

This line calls Nullpointer:   ws.url("https://...");ws is null by the time Guice instantiates the class Application. Moreover, the presence requestof a controller field is not thread safe. Change the code as follows:

public class Application extends Controller {

    private WSClient ws;

    @Inject
    public Application(WSClient ws) {

        this.ws = ws;
        WSRequest request = this.ws.url("https://...");

        ...
    }
}
+5
source

Source: https://habr.com/ru/post/1599511/


All Articles