How to specify variables correctly in Kotlin?

  • 0
    Good day. I use Kotlin and Spring. Tell me how to work with variables correctly (in the structure below) to avoid constant checking for NULL and prescribing "!!"?

    There are such variables in the class.

    @Autowired
        var newsModules: NewsModules
    
        @Autowired
        var usersModules: UsersModules
    
        @Autowired
        var regionsModules: RegionsModules
    
        @Autowired
        var productModule: ProductsModules
    
        @Autowired
        var requestsModules: RequestsModules


    If you do not specify " init "

    init {
            newsModules = NewsModules()
            usersModules = UsersModules()
            regionsModules = RegionsModules()
            productModule = ProductsModules()
            requestsModules = RequestsModules()
        }


    Then you need to initialize the variables in this way.

    @Autowired
        var newsModules: NewsModules? = null
    ...


    This entails constantly checking for " null " and prescribing " !! ". And as I understand it, using " init () " is not entirely correct here, since " @Autowired " is used.

    Tell me how to arrange it more competently. Thank you!
    Java Declan Elliott, Jul 15, 2019

  • 1 Answers
  • 0

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!