Created
April 14, 2016 23:41
-
-
Save blast-hardcheese/c7253bc3ab55b0a6e79e19169f60e8b5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Hey @Inject() (val app: Application) { | |
app.configuration // Guaranteed to work, since we can never get here unless we have enough contructor parameters | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Hey { | |
@Inject private var app: Application = _ | |
println("Before foo") | |
val foo = { | |
println("Inside foo") | |
app.configuration // Guaranteed to fail, since we're still in the class constructor, and the value starts as null (because Guice sets these values after initialization) | |
} | |
println("After foo") | |
} | |
/* | |
Output: | |
Asking for "Hey" | |
Before foo | |
Inside foo | |
[error] application - Unable to provision, see the following errors: | |
1) Error injecting constructor, java.lang.NullPointerException | |
at controllers.Hey.<init>(Main.scala:43) | |
while locating controllers.Hey | |
1 error (Error page was displayed to a user.) | Map(dynoName -> JsValueWrapperImpl(null)) | |
com.google.inject.ProvisionException: Unable to provision, see the following errors: | |
1) Error injecting constructor, java.lang.NullPointerException | |
at controllers.Hey.<init>(Main.scala:43) | |
while locating controllers.Hey | |
1 error | |
at com.google.inject.internal.InjectorImpl$2.get(InjectorImpl.java:1025) ~[guice-4.0.jar:na] | |
at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1051) ~[guice-4.0.jar:na] | |
at play.api.inject.guice.GuiceInjector.instanceOf(GuiceInjectorBuilder.scala:321) ~[play_2.11-2.4.6.jar:2.4.6] | |
at play.api.inject.guice.GuiceInjector.instanceOf(GuiceInjectorBuilder.scala:316) ~[play_2.11-2.4.6.jar:2.4.6] | |
at controllers.Main$$anonfun$testInternalServerError$1.apply(Main.scala:206) ~[classes/:2.4.6] | |
at controllers.Main$$anonfun$testInternalServerError$1.apply(Main.scala:204) ~[classes/:2.4.6] | |
at play.api.mvc.ActionBuilder$$anonfun$apply$16.apply(Action.scala:408) ~[play_2.11-2.4.6.jar:2.4.6] | |
at play.api.mvc.ActionBuilder$$anonfun$apply$16.apply(Action.scala:407) ~[play_2.11-2.4.6.jar:2.4.6] | |
at controllers.Actions$BaseAction$.invokeBlock(BaseController.scala:207) ~[classes/:2.4.6] | |
at controllers.Actions$BaseAction$.invokeBlock(BaseController.scala:203) ~[classes/:2.4.6] | |
Caused by: java.lang.NullPointerException: null | |
at controllers.Hey.<init>(Main.scala:38) ~[classes/:2.4.6] | |
at controllers.Hey$$FastClassByGuice$$e92610e5.newInstance(<generated>) ~[na:na] | |
at com.google.inject.internal.cglib.reflect.$FastConstructor.newInstance(FastConstructor.java:40) ~[guice-4.0.jar:na] | |
at com.google.inject.internal.DefaultConstructionProxyFactory$1.newInstance(DefaultConstructionProxyFactory.java:61) ~[guice-4.0.jar:na] | |
at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:105) ~[guice-4.0.jar:na] | |
at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:85) ~[guice-4.0.jar:na] | |
at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:267) ~[guice-4.0.jar:na] | |
at com.google.inject.internal.InjectorImpl$2$1.call(InjectorImpl.java:1016) ~[guice-4.0.jar:na] | |
at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1092) ~[guice-4.0.jar:na] | |
at com.google.inject.internal.InjectorImpl$2.get(InjectorImpl.java:1012) ~[guice-4.0.jar:na] | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Hey { | |
@Inject private var app: Application = _ | |
println("Before foo") | |
lazy val foo = { | |
println("Inside foo") | |
app.configuration | |
} | |
println("After foo") | |
val bar = foo.toString // No safety means careless or race-y calculations could cause unexpected failures | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment