Solved Need help calling bStats from another class

Discussion in 'Spigot Plugin Development' started by Reset65, 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
        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>(
      at io.karma.internal.modules.MetricsModule.class(
      while locating io.karma.internal.modules.MetricsModule

    1 error
            at ~[?:?]
            at$1.get( ~[?:?]
            at ~[?:?]
            at io.karma.modularity.loader.ModuleLoader.load( ~[?:?]
            at io.karma.modularity.loader.ModuleLoader.load( ~[?:?]
            at io.karma.KarmaAPI.onEnable( ~[?:?]
            at ~[spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin( [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin( [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins( [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.MinecraftServer.t( [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.MinecraftServer.l( [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.MinecraftServer.a( [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at net.minecraft.server.v1_12_R1.DedicatedServer.init( [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at [spigot-latest.jar:git-Spigot-2086bb0-8cc5a7e]
            at Source) [?:?]
    Caused by: java.lang.IllegalArgumentException: Plugin cannot be null!
            at io.karma.metrics.bukkit.Metrics.<init>( ~[?:?]
            at io.karma.internal.modules.MetricsModule.<init>( ~[?:?]
            at io.karma.internal.modules.MetricsModule$$FastClassByGuice$$b3f39990.newInstance(<generated>) ~[?:?]
            at$FastClassProxy.newInstance( ~[?:?]
            at ~[?:?]
            at ~[?:?]
            at$Factory.get( ~[?:?]
            at ~[?:?]
            at$1.get( ~[?:?]
            at ~[?:?]
            at$1.get( ~[?:?]
            ... 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!

  2. MiniDigger


    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


    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


    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
    The GitHub repo with tons of docs is located here
  11. I'm old school. I like my boiler plate code. :p