got an error `kotlin.UninitializedPropertyAccessEx...
# ktor
i
got an error
kotlin.UninitializedPropertyAccessException: lateinit property sqls has not been initialized
g
Maybe you forgot to call inject inside your class if you don't use constructor injection. Hard to tell anything without code
i
Code is really simple:
Copy code
package ktor.module

import io.ktor.application.Application
import io.ktor.application.call
import io.ktor.application.install
import io.ktor.features.*
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.request.path
import io.ktor.response.respondText
import io.ktor.routing.get
import io.ktor.routing.routing
import main.kotlin.di.DaggerSQLComponent
import main.kotlin.di.SQLHolder
import org.slf4j.event.Level
import javax.inject.Inject
import javax.inject.Named


class App{
    @Inject
    @Named("BlaBlaBlaSQLHolder")
    lateinit var sqlHolder: SQLHolder

    init{
        DaggerSQLComponent.create()
    }

    fun Application.rootModule() {
        install(CallLogging) {
            level = <http://Level.INFO|Level.INFO>
            filter { call -> call.request.path().startsWith("/") }
        }

        routing {
            get("/") {
                call.respondText("HELLO WORLD! ${sqlHolder.testSQL}", contentType = ContentType.Text.Plain)
            }

            install(StatusPages) {
                statusFile(HttpStatusCode.NotFound, filePattern = "error#.html")
            }
        }
    }
}
And here is an DI module/component:
Copy code
package main.kotlin.di

import dagger.Component
import dagger.Module
import dagger.Provides
import javax.inject.Named
import javax.inject.Singleton

@Component(modules = [SQLModule::class])
interface SQLComponent

@Module
class SQLModule{
    @Provides
    @Singleton
    @Named("BlaBlaBlaSQLHolder")
    fun povideTestSQLHolder() = SQLHolder()
}

class SQLHolder{
    val testSQL = "Im a test sql"
}
g
Exactly what I said. Because you don’t use constructor injection (what I personally recommend to use instead of field injection if it’s possibl), you have to call inject this class on init, something like:
DaggerSQLComponent.create().inject(this)
Still, I would implement it in a different way, on your Main.main() function I would create root app component which can provide App. And App contructor contains all required dependencies
i
Yay, it works Thank you a lot
👍 1