[Solved] @Inject annotations not injecting PluginContainer or Logger

Title says it all.
For some reason my @Inject annotations are not providing my plugin with a logger or a PluginContainer, and this is no bueno.

Code with point of failure:

@Plugin(
id=GeoWorldMain.ID,
name=GeoWorldMain.NAME,
version=GeoWorldMain.VERSION)
public class GeoWorldMain {

public static final String ID = "geoworld";
public static final String NAME = "GeoWorld";
public static final String VERSION = "1.0.2a";
@Inject 
private static PluginContainer container;
@Inject 
private static Logger logger;

public static PluginContainer GetPluginContainer() {
	if(container==null)
		logger.debug("isNull"); //**<-- it fails here with a nullPointer Error**
	else
		logger.debug("isNotNull");
	return container;
}

Is the Logger class the correct one? (org.slf4j.Logger)
And the Inject annotation?
And when do you call that (static) method? The plugin has to be constructed…

What implementation and version of Sponge are you using?

Also it might be because those fields are static, java - guice injection in static variable - Stack Overflow

1 Like

I do not think they can be static.

3 Likes

Damn you Ninja’d me Time, I was in the middle of editing and looking through the Guice documentation.

Upgrading this ‘don’t think’ to a ‘definitely can’t’. Injected fields depend on their container’s identity; statics don’t belong to any object and therefore can’t be deterministically created.

Yep. Consensus was static won’t work with injection… and not only does it make sense, but it fixed the issue. Make your plugin’s main class a singleton folks!

1 Like