I have a simple class like so:
class MyClass {
private static SomeClass object = null;
public void init(Someclass injectedObject) {
object = injectedObject;
}
}
I init this class in my Guice module:
public class MyModule extends AbstractModule {
@Provides @Singleton
public SomeClass getStuff() {
SomeClass injectObject = new SomeClass();
MyClass.init(injectObject);
}
}
But when I package the my jar and submit it as a job to Flink runtime, I get an error saying NullPointerException: object in MyClass in null
I have even tried adding this to my method:
public static void main(String[] args) {
// Guice stuff
}
public void doStuff() {
MyClass.init(injectedObject);
}
Am I missing something? Why is an initialized variable getting reset to null in Flink runtime?
There's a better way to inject into static fields using Guice:
However, it would be even better if you can omit the
staticfield altogether:Now you can request
MyClassinstances either by injecting them or requesting them directly from theinjector, and they will all have access to the sameSomeClassinstance.