Guice 绑定中的 NullPointerException。我是否错误地绑定了 HelperFactory?
这是以下代码,我在其中收到 NullPointerException。 HelperFactory 是一个 Client 类,我无法更改它。我是 Guice 的新手,浏览文档并不能帮助理解我哪里出错了。
public class ModuleA extends AbstractModule {
@Override
protected void configure() {
super.configure();
bind(HelperFactory.class).toInstance(HelperFactory.getInstance());
@Provides
public DoSomethingClient getDoSomethingClient() {
return DoSomethingClient.getInstance();
}
}
}
class DoSomethingClient {
public static DoSomethingClient getInstance() {
var helperFactory = HelperFactory.getInstance();
return new DoSomethingClient(helperFactory.getClient());
}
}
Stack Trace:
Error in custom provider, java.lang.NullPointerException
at com.myApp.abc.injection.ModuleA.DoSomethingClient(ModuleA.java:22)
Here is the following code, where I am getting a NullPointerException. HelperFactory is a Client class, which I cannot change. I am new to Guice, and going through the documentation did not help in understanding where I am going wrong.
public class ModuleA extends AbstractModule {
@Override
protected void configure() {
super.configure();
bind(HelperFactory.class).toInstance(HelperFactory.getInstance());
@Provides
public DoSomethingClient getDoSomethingClient() {
return DoSomethingClient.getInstance();
}
}
}
class DoSomethingClient {
public static DoSomethingClient getInstance() {
var helperFactory = HelperFactory.getInstance();
return new DoSomethingClient(helperFactory.getClient());
}
}
Stack Trace:
Error in custom provider, java.lang.NullPointerException
at com.myApp.abc.injection.ModuleA.DoSomethingClient(ModuleA.java:22)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有一种感觉,您使用 Guice 的方式是错误的。或者这张票上没有足够的信息来说明任何事情。
但是,这个问题似乎与 Guice 无关。
这段代码中的哪一行给你抛出了一个空指针?
是吗
返回新的DoSomethingClient(helperFactory.getClient());
?如果是这种情况,
HelperFactory.getInstance();
不会返回任何对象。您可能想问为什么是null
?好吧,Guice 并没有在任何地方实例化该对象。因此,无论是在HelperFactory
中实例化和设置该对象的代码都可能尚未被调用。I have a feeling that you are using Guice in a wrong way. Or there is not enough information on this ticket to say anything.
But, it seems like the issue is not related to Guice.
Which exact line in this code is throwing you a Nullpointer?
is it
return new DoSomethingClient(helperFactory.getClient());
?if that is the case,
HelperFactory.getInstance();
did not return any object. You might want to ask why is thatnull
? Well, Guice is not instantiating that object anywhere. So, whichever the code that is instantiating and setting that object in theHelperFactory
is probably not invoked yet.