Home > OS >  java.lang.ExceptionInInitializerError: null in minecraft kotlin plugin
java.lang.ExceptionInInitializerError: null in minecraft kotlin plugin

Time:11-03

I've got problem while I code minecraft plugin with kotlin. Here's error message:

[12:37:07 ERROR]: Error occurred while enabling WorkAndGambling v1.0-SNAPSHOT (Is it up to date?)
java.lang.ExceptionInInitializerError: null
        at io.github.buudd.wag.Main.onEnable(Main.kt:10) ~[WorkAndGambling.jar:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[patched_1.17.1.jar:git-Paper-352]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:370) ~[patched_1.17.1.jar:git-Paper-352]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:500) ~[patched_1.17.1.jar:git-Paper-352]
        at org.bukkit.craftbukkit.v1_17_R1.CraftServer.enablePlugin(CraftServer.java:561) ~[patched_1.17.1.jar:git-Paper-352]
        at org.bukkit.craftbukkit.v1_17_R1.CraftServer.enablePlugins(CraftServer.java:475) ~[patched_1.17.1.jar:git-Paper-352]
        at net.minecraft.server.MinecraftServer.loadWorld(MinecraftServer.java:730) ~[patched_1.17.1.jar:git-Paper-352]
        at net.minecraft.server.dedicated.DedicatedServer.initServer(DedicatedServer.java:317) ~[patched_1.17.1.jar:git-Paper-352]
        at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:1217) ~[patched_1.17.1.jar:git-Paper-352]
        at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:319) ~[patched_1.17.1.jar:git-Paper-352]
        at java.lang.Thread.run(Thread.java:831) ~[?:?]
Caused by: java.lang.IllegalArgumentException: Plugin already initialized!
        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:229) ~[patched_1.17.1.jar:git-Paper-352]
        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:53) ~[patched_1.17.1.jar:git-Paper-352]
        at io.github.buudd.wag.Main.<init>(Main.kt:7) ~[WorkAndGambling.jar:?]
        at io.github.buudd.wag.FunctionsKt.<clinit>(functions.kt:12) ~[WorkAndGambling.jar:?]
        ... 11 more
Caused by: java.lang.IllegalStateException: Initial initialization
        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:232) ~[patched_1.17.1.jar:git-Paper-352]
        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:53) ~[patched_1.17.1.jar:git-Paper-352]
        at io.github.buudd.wag.Main.<init>(Main.kt:7) ~[WorkAndGambling.jar:?]
        at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:?]
        at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:78) ~[?:?]
        at jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:?]
        at java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) ~[?:?]
        at java.lang.reflect.ReflectAccess.newInstance(ReflectAccess.java:128) ~[?:?]
        at jdk.internal.reflect.ReflectionFactory.newInstance(ReflectionFactory.java:350) ~[?:?]
        at java.lang.Class.newInstance(Class.java:642) ~[?:?]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:83) ~[patched_1.17.1.jar:git-Paper-352]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:153) ~[patched_1.17.1.jar:git-Paper-352]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:414) ~[patched_1.17.1.jar:git-Paper-352]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:322) ~[patched_1.17.1.jar:git-Paper-352]
        at org.bukkit.craftbukkit.v1_17_R1.CraftServer.loadPlugins(CraftServer.java:419) ~[patched_1.17.1.jar:git-Paper-352]
        at net.minecraft.server.dedicated.DedicatedServer.initServer(DedicatedServer.java:287) ~[patched_1.17.1.jar:git-Paper-352]
        ... 3 more

I noticed that error occured at Main.kt, line 10. And that was loadConfig() function. So, I looked Functions.kt:

package io.github.buudd.wag

import org.bukkit.configuration.file.YamlConfiguration
import io.github.buudd.wag.Variables.cfile
import io.github.buudd.wag.Variables.file


fun loadConfig() {
    cfile = YamlConfiguration.loadConfiguration(file)
    try {
        if (!file.exists()) {
            cfile!!.save(file)
        }
        cfile!!.load(file)
    } catch (localException: Exception) {
        localException.printStackTrace()
    }
}

And variables. Variables.kt:

import org.bukkit.configuration.file.YamlConfiguration
import org.bukkit.plugin.java.JavaPlugin
import java.io.*
import java.util.*


object Variables {
    private val plugin: JavaPlugin = Main()
    var moneyMap: MutableMap<UUID?, Int?> = mutableMapOf(null to null)
    var joinMap: MutableMap<UUID?, Int?> = mutableMapOf(null to null)
    val file: File = File("plugins/WorkAndGambling/config.yml")
    var cfile: YamlConfiguration? = null
}

Main.kt

package io.github.buudd.wag

import io.github.buudd.wag.commands.WAGcommand
import io.github.buudd.wag.listener.Listeners
import org.bukkit.plugin.java.JavaPlugin

class Main : JavaPlugin() {
    override fun onEnable() {
        logger.info("Enabling...")
        loadConfig()
        getCommand("sell")?.apply {
            setExecutor(WAGcommand(this@Main))
            tabCompleter = WAGcommand(this@Main)
        }
        server.pluginManager.registerEvents(Listeners(), this)
    }

    override fun onDisable() {
        logger.info("Disabling...")
    }
}

But, I cannot find why error occured. Sorry if this code's problem is just my mistake.

CodePudding user response:

The plugin is already enabled.

This can be caused because :

  • The same plugin is already on the server, but with another JAR name

To fix it, just remove the old jar.

  • Another plugin use the same name

To fix it, go in plugin.yml and change name variable.

  • You creating a new instance of this plugin, and JavaPlugin interface creating a new one. This is caused in Variables class. How to fix it ?

You should set the instance as static, go transfer it in method parameter/constructor.

Example of "instance as static" :

companion object {
   lateinit var plugin: Main
}

override fun onEnable() {
   plugin = this;
}

Source of static way

Example of "Transfer in param/constructor" :

It depend of how you are using this variable. You seems to already do it for the class WAGcommand.

PS: I'm a Java spigot developer, so maybe there is issue in kotlin code.

  • Related