Is that behavior intended? I don't understand why ...
# announcements
m
Is that behavior intended? I don't understand why my val fields are null here o.O It's a data class inside a data class
i
@Mikołaj Karwowski Hi! It is a bug in IDE debugger - https://youtrack.jetbrains.com/issue/KT-35570
m
But shouldn't they be initialized, as I assign them a value by the declaration? It's kind of weird for me that I get an NPE when calling a private method using local vals - that I think should be present with object creation
i
Maybe I misunderstood you. Can you attach a sample of your code to reproduce the problem?
m
Sure
Copy code
import com.google.gson.Gson
import org.junit.jupiter.api.Test
import java.text.SimpleDateFormat
import java.util.*

class SampleTest {

    val json: String = """
                    {
                        id: 0,
                        data: "I dat",
                        childList: [
                            {
                            id: 0,
                            timestamp: "12903240834",
                            body: "I bod"
                            },
                            {
                            id: 0,
                            timestamp: "12903240834",
                            body: "I bod"
                            },
                            {
                            id: 0,
                            timestamp: "12903240834",
                            body: "I bod"
                            }
                        ]
                    }
                """.trimIndent()

    @Test
    fun sampleTest() {
        val parent = Gson().fromJson<ParentDataClass>(json, ParentDataClass::class.java)

        parent.childList.map {
            println("Map: $it")
            it.foo()
        }
    }
    
    data class ParentDataClass(
            val id: Int,
            val data: String,
            val childList: List<ChildDataClass>
    ) {

        data class ChildDataClass(
                val id: Int,
                val timestamp: String,
                val body: String
        ) {

            val datePattern = "yyyyMMddHHmmssSSS"

            fun foo() {
                println("Date pattern: $datePattern")
                val dateFormat = SimpleDateFormat(datePattern, Locale.ENGLISH)
                dateFormat.timeZone = TimeZone.getDefault()
            }
        }
    }
}
(I don't know if that's the best way of sharing code on slack, but here ya go :P)
👀 1
i
Looks like it's GSON problem, see the similar problem with default value in their github https://github.com/google/gson/issues/1550
This code works fine:
Copy code
val parent = ParentDataClass(0, "I dat", listOf(
    ParentDataClass.ChildDataClass(0, "12903240834", "I bod"),
    ParentDataClass.ChildDataClass(0, "12903240834", "I bod"),
    ParentDataClass.ChildDataClass(0, "12903240834", "I bod")
))
m
Aye, I did notice without GSON those fields were available, but didn't suppose that could be a library thing - that a library could overshadow an non default val with assignment that - I think - has nothing to do with the process of parsing it. Very concerning in terms of unpredictability... So in terms of Kotlin it is a normal thing that a - non default - val can be modified by outside logic? I ve always considered vals a 100% predictable and accessible element of my classes... Was I wrong then?
i
Can you please create an issue — http://kotl.in/issue
m