CreationException: Unable to create injector error in Play 2.5.18 in replacement for GlobalSettings Java code using com.google.inject.AbstractModule

19,500

The configuration must be injected into the constructor

@Inject
public OnStart(Config config) {
Share:
19,500
Dan
Author by

Dan

Updated on June 11, 2022

Comments

  • Dan
    Dan almost 2 years

    I am upgrading my Play application from 2.5.12 to 2.5.18 and when I start up the application (using sbt), I am receiving this error:

    CreationException: Unable to create injector, see the following errors:

    1) Error injecting constructor, java.lang.NullPointerException
      at modules.OnStart.<init>(OnStart.java:15)
      at modules.Global.configure(Global.java:9) (via modules: com.google.inject.util.Modules$OverrideModule -> modules.Global)
      while locating modules.OnStart
    
    1 error
    

    The rest of the error is:

    No source available, here is the exception stack trace:
    ->com.google.inject.CreationException: Unable to create injector, see the following errors:
    
    1) Error injecting constructor, java.lang.NullPointerException
      at modules.OnStart.<init>(OnStart.java:15)
      at modules.Global.configure(Global.java:9) (via modules: com.google.inject.util.Modules$OverrideModule -> modules.Global)
      while locating modules.OnStart
    
    1 error
         com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:470)
         com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:184)
         com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:110)
         com.google.inject.Guice.createInjector(Guice.java:99)
         com.google.inject.Guice.createInjector(Guice.java:84)
         play.api.inject.guice.GuiceBuilder.injector(GuiceInjectorBuilder.scala:181)
         play.api.inject.guice.GuiceApplicationBuilder.build(GuiceApplicationBuilder.scala:137)
         play.api.inject.guice.GuiceApplicationLoader.load(GuiceApplicationLoader.scala:21)
         play.core.server.DevServerStart$$anon$1.$anonfun$get$6(DevServerStart.scala:171)
         play.utils.Threads$.withContextClassLoader(Threads.scala:21)
         play.core.server.DevServerStart$$anon$1.$anonfun$get$3(DevServerStart.scala:168)
         scala.Option.map(Option.scala:146)
         play.core.server.DevServerStart$$anon$1.$anonfun$get$2(DevServerStart.scala:133)
         scala.util.Success.flatMap(Try.scala:247)
         play.core.server.DevServerStart$$anon$1.$anonfun$get$1(DevServerStart.scala:131)
         scala.concurrent.Future$.$anonfun$apply$1(Future.scala:655)
         scala.util.Success.$anonfun$map$1(Try.scala:251)
         scala.util.Success.map(Try.scala:209)
         scala.concurrent.Future.$anonfun$map$1(Future.scala:289)
         scala.concurrent.impl.Promise.liftedTree1$1(Promise.scala:29)
         scala.concurrent.impl.Promise.$anonfun$transform$1(Promise.scala:29)
         scala.concurrent.impl.CallbackRunnable.run(Promise.scala:60)
         scala.concurrent.impl.ExecutionContextImpl$AdaptedForkJoinTask.exec(ExecutionContextImpl.scala:140)
         java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
         java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
         java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
         java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
    

    I created new modules to replace the deprecated GlobalSettings based on this post:

    https://www.playframework.com/documentation/2.5.x/GlobalSettings

    Here are the classes mentioned in the error:

    Global.java

    package modules;
    
    import com.google.inject.AbstractModule;
    
    public class Global extends AbstractModule {
    
        @Override
        public void configure() {
            bind(OnStart.class).asEagerSingleton();
            bind(OnStop.class).asEagerSingleton();
        }
    
    }
    

    OnStart.java

    package modules;
    
    import javax.inject.Inject;
    import javax.inject.Singleton;
    
    import jobs.JobControl;
    import com.typesafe.config.Config;
    import play.Logger;
    
    @Singleton
    public class OnStart {
    
        @Inject
        public OnStart(Config config) {
            Logger.debug("Application started...");     
            // Turn on scheduler?
            String schedulerEnabled;
            String dueDateRun;
            schedulerEnabled = config.getString("scheduler.enabled");
            dueDateRun = config.getString("duedate.run.flag");
            if (schedulerEnabled.equals("true")) {
                //JobControl.cleanupTables();
                //JobControl.emptyRecycleBin();
                if (dueDateRun.equals("on")) {
                    JobControl.dueDateNotifications();
                }
            }
        }
    }
    

    I am not sure what to do to fix this.

  • Dan
    Dan almost 6 years
    I removed the @Inject but am still getting the same error.
  • Andriy Kuba
    Andriy Kuba almost 6 years
    try to check the code inside onStart - maybe the configuration is absent. I do not remember if you need to inject it in this version or a little bit later: playframework.com/documentation/2.5.x/…
  • Dan
    Dan almost 6 years
    I updated the OnStart.java code above to use the @Inject the Config. But I am still getting the same error. I appreciate you hanging in here with me. Looking for some other ideas.
  • Andriy Kuba
    Andriy Kuba almost 6 years
    Your update definitely will give en exception, the field is injeced after the constructor, so change the code so it takes config as a constructor parameter: @Inject public OnStart(Config config)