Q:

IsInitialized is not being called in Kotlin

I don't have errors in my logs but I cannot hit the debugger inside my if(::db.isinitialized) condition
My code is below:
MainActivity
class MainActivity : AppCompatActivity() {

    lateinit var db: DatabaseCommon;

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        if(::db.isInitialized) {
            val dbVersion = DatabaseCommon.myInstance.dbVersion;
            val dbName = DatabaseCommon.myInstance.dbName;
            val tableName = DatabaseCommon.myInstance.tableName;
            val name: String = DatabaseCommon.myInstance.name;
            val age: String = DatabaseCommon.myInstance.age;

            try{
                val db = this.openOrCreateDatabase(dbName, Context.MODE_PRIVATE, null)
                var query = "CREATE TABLE IF NOT EXISTS  ${tableName} (${name} VARCHAR, ${age} INT(2))";
                db.execSQL(query)

                query = "INSERT INTO ${tableName} (${name}, ${age}) VALUES ('James', 20)";
                db.execSQL(query);

                Toast.makeText(applicationContext, "CREATED", Toast.LENGTH_LONG);
            }catch(e: Exception) {
                e.printStackTrace();
            }
        }
    }
}

DatabaseCommon
class DatabaseCommon {

    val dbVersion: Int = 1;
    val dbName: String = "db_munsicians";

    val tableName: String = "musicians"
    val name: String = "name";
    var age: String = "age";

    private constructor() {}

    companion object {
        private lateinit var instance: DatabaseCommon;

        val myInstance: DatabaseCommon
            get() {
                if (instance == null) {
                    instance = DatabaseCommon();
                }
                return instance;
            }
    }

    fun returnDbClass() {
        println("I was called");
    }
}

If I don't put the if(::db.isinitialized) I get an error of lateinit has not been initialized. How can I resolve this?

A:

You can't check if instance is not initialized using instance == null check. When you try to compare instance to null, you try to get a value of instance, but it's not initialized, to it throws an exception.
For your case, you should use lazy property instead of lateinit one. You may implement it this way:
private val instance: Lazy<DatabaseCommon> = lazy { DatabaseCommon() }

val myInstance: DatabaseCommon
    get() = instance.value

or this way:
val myInstance: Lazy<DatabaseCommon> = lazy { DatabaseCommon() }

