Guice 绑定中的 NullPointerException。我是否错误地绑定了 HelperFactory?

发布于 2025-01-14 03:48:07 字数 932 浏览 3 评论 0原文

这是以下代码,我在其中收到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

箜明 2025-01-21 03:48:07

我有一种感觉,您使用 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 that null ? Well, Guice is not instantiating that object anywhere. So, whichever the code that is instantiating and setting that object in the HelperFactory is probably not invoked yet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文