java.lang.ExceptionInInitializerError: null in minecraft kotlin plugin

java.lang.ExceptionInInitializerError: null in minecraft kotlin plugin

我在使用 kotlin 编写 minecraft 插件时遇到问题。这是错误消息:

[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[=10=](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

我注意到错误发生在 Main.kt,第 10 行。那是 loadConfig() 函数。所以,我看了 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()
    }
}

和变量。 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...")
    }
}

但是,我找不到错误发生的原因。抱歉,如果这段代码的问题只是我的错误。

插件已经启用。

这可能是因为:

  • 相同的插件已在服务器上,但具有另一个 JAR 名称

要修复它,只需删除旧的 jar。

  • 另一个插件使用相同的名称

要修复它,请进入 plugin.yml 并更改 name 变量。

  • 您正在创建此插件的一个新实例,并且 JavaPlugin 界面正在创建一个新实例。这是在 Variables class 中引起的。如何解决?

您应该将实例设置为静态,然后在方法parameter/constructor中传输它。

“静态实例”示例:

companion object {
   lateinit var plugin: Main
}

override fun onEnable() {
   plugin = this;
}

“在 param/constructor 中转账”示例:

这取决于您如何使用这个变量。您似乎已经为 class WAGcommand.

做到了

PS:我是 Java spigot 开发人员,所以可能 kotlin 代码中存在问题。