使用Java MessagesApi播放2.5.18应用程序导致意外异常ExceptionInInitializerError:null错误

时间:2018-04-09 13:57:21

标签: java playframework

我试图将我的应用程序从2.5.12移动到2.5.18,然后移到2.6.x.我正在使用MessagesApi,因为我读到旧消息被弃用了。

我找到了这篇文章,但由于它是Scala,它对我没有帮助:

Play 2.4.x how use Messages and not MessagesApi I18N

我有一个控制器Application.java,我已经设置了MessagesApi和Messages对象,并且在编译时没有收到错误,但在尝试运行应用程序时收到此错误:

[error] application -

! @77h0ppgdc - Internal server error, for (GET) [/] ->

play.api.UnexpectedException: Unexpected exception[ExceptionInInitializerError: null]
        at play.core.server.DevServerStart$$anon$1.$anonfun$get$3(DevServerStart.scala:187)
        at scala.Option.map(Option.scala:146)
        at play.core.server.DevServerStart$$anon$1.$anonfun$get$2(DevServerStart.scala:133)
        at scala.util.Success.flatMap(Try.scala:247)
        at play.core.server.DevServerStart$$anon$1.$anonfun$get$1(DevServerStart.scala:131)
        at scala.concurrent.Future$.$anonfun$apply$1(Future.scala:655)
        at scala.util.Success.$anonfun$map$1(Try.scala:251)
        at scala.util.Success.map(Try.scala:209)
        at scala.concurrent.Future.$anonfun$map$1(Future.scala:289)
        at scala.concurrent.impl.Promise.liftedTree1$1(Promise.scala:29)
Caused by: java.lang.ExceptionInInitializerError: null
        at controllers.Application$$FastClassByGuice$$b5b6aa19.newInstance(<generated>)
        at com.google.inject.internal.DefaultConstructionProxyFactory$FastClassProxy.newInstance(DefaultConstructionProxyFactory.java:89)
        at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:111)
        at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:90)
        at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:268)
        at com.google.inject.internal.SingleParameterInjector.inject(SingleParameterInjector.java:38)
        at com.google.inject.internal.SingleParameterInjector.getAll(SingleParameterInjector.java:62)
        at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:110)
        at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:90)
        at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:268)
Caused by: java.lang.NullPointerException: null
        at controllers.Application.<clinit>(Application.java:75)
        at controllers.Application$$FastClassByGuice$$b5b6aa19.newInstance(<generated>)
        at com.google.inject.internal.DefaultConstructionProxyFactory$FastClassProxy.newInstance(DefaultConstructionProxyFactory.java:89)
        at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:111)
        at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:90)
        at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:268)
        at com.google.inject.internal.SingleParameterInjector.inject(SingleParameterInjector.java:38)
        at com.google.inject.internal.SingleParameterInjector.getAll(SingleParameterInjector.java:62)
        at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:110)
        at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:90)

我正在看的那条线:

at controllers.Application.<clinit>(Application.java:75)

以下是摘录:

import play.i18n.Lang;
import play.i18n.Messages;
import play.i18n.MessagesApi;

public class Application extends Controller {

    @Inject
    MailerClient mailerClient;

    @Inject
    FormFactory formFactory;

    @Inject
    static MessagesApi messagesApi;

    static Collection<Lang> candidates = Collections.singletonList(new Lang(Locale.US));
    static Messages messages = messagesApi.preferred(candidates);
    ...
}

Application.java控制器中的位置是:

static Messages messages = messagesApi.preferred(candidates);

我根据这个文档构建了代码: https://www.playframework.com/documentation/2.6.x/JavaI18N#Externalizing-messages

我不确定如何解决这个问题。我很感激帮助。

2 个答案:

答案 0 :(得分:0)

我真的不知道Guice是如何工作的,但它不应该只注入实例吗?

换句话说,你真的需要static个字段吗?我会删除以上所有static修饰符。

答案 1 :(得分:0)

感谢@Frederic A.!我将我的代码更新为:

public class Application extends Controller {

    private final MailerClient mailerClient;
    private final FormFactory formFactory;
    private final MessagesApi messagesApi;

    private final Collection<Lang> candidates;
    private final Messages messages;

    @Inject
    public Application(MailerClient mailerClient, FormFactory formFactory, MessagesApi messagesApi) {
        this.mailerClient = mailerClient;
        this.formFactory = formFactory;
        this.messagesApi = messagesApi;

        this.candidates = Collections.singletonList(new Lang(Locale.US));
        this.messages = messagesApi.preferred(candidates);
    }
...
}

现在应用程序正在运行。