Skip to content

Error when using logger delegate in top-level function #65

Open

Description

@Tisawesomeness

When using the SLF4J delegate in a top-level function:

// file:Bootstrap.kt
val log by SLF4J

fun main() {
  log.info("Starting...")
  // ...
}

The code compiles but causes a runtime error:

Exception in thread "main" java.lang.NullPointerException
	at dev.minn.jda.ktx.util.SLF4J.getValue(proxies.kt:63)
	at codes.tis.minecord.BootstrapKt.getLog(Bootstrap.kt:10)
	at codes.tis.minecord.BootstrapKt.main$lambda$0(Bootstrap.kt:13)

To prevent this, you can change thisRef: Any? to thisRef: Any which will cause a compile error when used in a top-level function. Users can instead use val log by SLF4J("name") or val log by SLF4J<Class>.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions