Solved Need help calling bStats from another class

Discussion in 'Spigot Plugin Development' started by JarynME, Jun 5, 2018.

  1. Hi,

    I know using a constructor would allow me to pull my main class into bStats, but for some reason i'm getting
    Code (Java):
    java.lang.IllegalArgumentException: Plugin cannot be null!
    Here's my constructor in my main class
    Code (Java):
        /**
         * Constructor.
         */

        public KarmaAPI() {
        }
    and here's my metrics class
    Code (Java):
    @Module(name = "Metrics Module", desc = "A test module")
    public class MetricsModule {

        private KarmaAPI karmaAPI;

        // Setup metrics
        @Inject
        public MetricsModule() {
            // Start bStats metrics
            Metrics bStats = new Metrics(karmaAPI);
        }
    }
    I'm using @Desetude Modularity - API to inject the class into the onEnable, which uses guice.

    Here's the full error that i'm getting.
    Code (Java):
    1) Error injecting constructor, java.lang.IllegalArgumentException: Plugin cannot be null!
      at io.karma.internal.modules.MetricsModule.<init>(MetricsModule.java:16)
      at io.karma.internal.modules.MetricsModule.class(MetricsModule.java:16)
      while locating io.karma.internal.modules.MetricsModule

    1 error
            at com.google.inject.internal.InternalProvisionException.toProvisionException(InternalProvisionException.java:226) ~[?:?]
            at com.google.inject.internal.InjectorImpl$1.get(InjectorImpl.java:1053) ~[?:?]
            at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1086) ~[?:?]
            at io.karma.modularity.loader.ModuleLoader.load(ModuleLoader.java:129) ~[?:?]
            at io.karma.modularity.loader.ModuleLoader.load(ModuleLoader.java:80) ~[?:?]
            at io.karma.KarmaAPI.onEnable(KarmaAPI.java:30) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:403) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:381) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:330) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.MinecraftServer.t(MinecraftServer.java:422) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.MinecraftServer.l(MinecraftServer.java:383) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.MinecraftServer.a(MinecraftServer.java:338) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:272) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:545) [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at java.lang.Thread.run(Unknown Source) [?:?]
    Caused by: java.lang.IllegalArgumentException: Plugin cannot be null!
            at io.karma.metrics.bukkit.Metrics.<init>(Metrics.java:77) ~[?:?]
            at io.karma.internal.modules.MetricsModule.<init>(MetricsModule.java:18) ~[?:?]
            at io.karma.internal.modules.MetricsModule$$FastClassByGuice$$b3f39990.newInstance(<generated>) ~[?:?]
            at com.google.inject.internal.DefaultConstructionProxyFactory$FastClassProxy.newInstance(DefaultConstructionProxyFactory.java:89) ~[?:?]
            at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:114) ~[?:?]
            at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:91) ~[?:?]
            at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:306) ~[?:?]
            at com.google.inject.internal.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:40) ~[?:?]
            at com.google.inject.internal.SingletonScope$1.get(SingletonScope.java:148) ~[?:?]
            at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:39) ~[?:?]
            at com.google.inject.internal.InjectorImpl$1.get(InjectorImpl.java:1050) ~[?:?]
            ... 15 more
    Any ideas are welcomed! It's been about 2 years since I programmed in Java, so i'm a little rusty, hopefully i'm not missing something extremely obvoius!

    Thanks!
     
  2. MiniDigger

    Supporter

    In order to use guice you need to either annotate the field or provide a proper injection constructor with the right parameters.
     
    • Like Like x 1
  3. I'm still getting the same error, I annotated the field, and constructor. I check guice and followed there documentation and i'm still getting the error. Any more ideas?
     
  4. karmaAPI is null..... You never passed a reference to it from your main class.
     
  5. I did do that, but I got a different error, saying that the plugin was initialized more than once
     
  6. Don't do new KarmaAPI().... Pass the reference
     
  7. MiniDigger

    Supporter

    He is trying to use guice.

    Do either the field or a constructor with the proper parameters, not both
     
    • Winner Winner x 1
  8. Oh. No idea what that even is. I'll show myself the door.
     
    • Funny Funny x 1
  9. You did still help me find a fix for another project i'm working on, so I do appreciate it!
    Also thank you for your help, I got it working now!
     
    • Friendly Friendly x 1
  10. MiniDigger

    Supporter

    It's an dependency injection framework that resolves fields and constructors annotated with @inject and fills them with the right values. So instead of passing references manually guice does that for you.
    You should check it out when you find time, mrdienns wrote a small introduction
    https://www.spigotmc.org/threads/tutorial-spigot-plugins-dependency-injection.295218/
    The GitHub repo with tons of docs is located here
    https://github.com/google/guice
     
  11. I'm old school. I like my boiler plate code. :p