Skip to content

ERROR_INJECTING_METHOD

Googler edited this page Aug 13, 2020 · 1 revision

ERROR_INJECTING_METHOD

Summary

Guice will throw an ERROR_INJECTING_METHOD error if a method that uses method injection throws an exception when called by Guice.

Example:

final class Foo {
  private Bar bar;

  @Inject
  void setBar(Bar bar) {
    this.bar = bar;
    this.bar.getBaz().doSomething(); // getBaz() may return null.
  }
}

When Bar.getBaz() returns null, a NullPointerException will be thrown from the setBar method causing Guice to throw an ERROR_INJECTING_METHOD error.

Common Causes

Unexpected exception in injected methods

The fix for the Guice ERROR_INJECTING_METHOD error is to avoid throwing exceptions from injected methods:

final class Foo {
  private Bar bar;

  @Inject
  void setBar(Bar bar) {
    this.bar = bar;
    if (this.bar != null) {
       this.bar.getBaz().doSomething();
    }
  }
}
Clone this wiki locally