Multiple Listeners in Kotlin

Discussion in 'Plugin Development' started by Cruxial, Oct 14, 2021.

  1. Offline


    Hey, I have recently started getting into Minecraft Plugin Development, and I am currently trying to implement a handful of listeners in my project. Though, I can't seem to make this work in Kotlin. I stayed up until like 2am last night trying to fix this lol.

    I keep getting a null reference error, and I'm really not sure what's causing it. The EventHandler in my main class seems to work fine, but whenever I try to extend it to external classes, I get the null reference error I was talking about.

    Main class:
    class MinecraftStatsListener : JavaPlugin(), Listener {
        override fun onEnable() {
            // Plugin startup logic
            send("Plugin Enabled.");
            val pm = Bukkit.getServer().pluginManager
            val plugin = this;
            val join = Join(this)
            pm.registerEvents(this, this)
            //pm.registerEvents(join, plugin)
        public fun onBlockBreak(e: BlockBreakEvent) {
            val p = e.player
            val b = e.block
            send("${b.type} broken by ${p.displayName}!")
            send("${p.displayName} joined the game.")
        override fun onDisable() {
            // Plugin shutdown logic
            send("Plugin Disabled.")
    Listener Class:
    public class Join constructor(i: MinecraftStatsListener) : Listener {
        init {
            i.server.pluginManager.registerEvents(this, i)
        public fun onLogin(e: PlayerJoinEvent) {
            val user: PluginUser = PluginUser()
            user.uuid = e.player.uniqueId
   = e.player.displayName
            user.playerTime = e.player.playerTime
            val apiRequest: PluginUserRequest = PluginUserRequest()
            send("${} joined the server.")
  2. Offline


    Show the error.

    It is probably either:
    • Your listener class does not have a default constructor
    • The way you pass in and interact with "i" in the second class
    Last edited: Oct 14, 2021
  3. Offline


    [18:20:55] [Server thread/ERROR]: Could not pass event PlayerJoinEvent to MinecraftStatsListener v1.0-SNAPSHOT
    org.bukkit.event.EventException: null
            at$1.execute( ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at org.bukkit.plugin.RegisteredListener.callEvent( ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at org.bukkit.plugin.SimplePluginManager.fireEvent( ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at org.bukkit.plugin.SimplePluginManager.callEvent( ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at net.minecraft.server.players.PlayerList.a( ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at net.minecraft.server.MinecraftServer.b( ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at net.minecraft.server.dedicated.DedicatedServer.b( ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at net.minecraft.server.MinecraftServer.a( ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at net.minecraft.server.MinecraftServer.x( ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at net.minecraft.server.MinecraftServer.lambda$0( ~[spigot-1.17.1.jar:3258-Spigot-dc75aca-960f310]
            at [?:?]
  4. Online

    timtower Administrator Administrator Moderator

    @Cruxial Any reason why you are using Kotlin? Doesn't make debugging easier...
  5. Offline


    @Cruxial we're gonna need line numbers of your code also.
    Did you happen to see the edits to my last post?
  6. Offline


    Yeah I saw it now. I posted my reply a few minutes after you made your's. Moderator approvement just took a while.
    I think I might have found my issue by looking at a refrence plugin I found on github.
    I'll update the thread when I get to test my supposed solution.

    I use Kotlin for the features and its simplified language structure.
    I understand that it can come with complications with Bukkit though, and I'm willing to suffer through that lol

    Alright, so I have tried the stuff I found from the reference plugin, but to no avail.

    Regarding this, I have tried registering the plugins in my main class, also didn't work.
    I am still getting the same error posted earlier, but yeah, Kotlin stacktraces aren't exactly helpful...

    I'll be posting my current code in 2 seperate pastebin links so you can see the line numbers.

    Main class
    Listener class

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Oct 15, 2021
  7. Offline


    @Cruxial it could be this line right here.
    I'm sure Join is not instantiated correctly there but i could be wrong.
  8. Offline


    Yeah, Kotlin does not have the parentheses as class instantiators.

Share This Page