Notice
Recent Posts
Recent Comments
Link
ยซ   2025/04   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Tags
more
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

๐ŸŒฑ dreaming DiNO

[Kotlin] [์—๋Ÿฌํ•ด๊ฒฐ] Gson TypeConverter ์‹œ Null ์ฒ˜๋ฆฌ (Nullable coulmn ์ถ”๊ฐ€) ๋ณธ๋ฌธ

Android/Android Studio

[Kotlin] [์—๋Ÿฌํ•ด๊ฒฐ] Gson TypeConverter ์‹œ Null ์ฒ˜๋ฆฌ (Nullable coulmn ์ถ”๊ฐ€)

MK_____ 2022. 5. 16. 09:35

๐Ÿฅบ ์—๋Ÿฌ ๋ฐœ์ƒ

    java.lang.RuntimeException: Exception while computing database live data.
        at androidx.room.RoomTrackingLiveData$1.run(RoomTrackingLiveData.java:92)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)
     Caused by: java.lang.NullPointerException: gson.fromJson(value, Array<String>::class.java) must not be null
        at com.example.memolog.repository.StringListTypeConverter.jsonToList(StringListTypeConverter.kt:21)
        at com.example.memolog.repository.dao.MemoDao_Impl$14.call(MemoDao_Impl.java:414)
        at com.example.memolog.repository.dao.MemoDao_Impl$14.call(MemoDao_Impl.java:345)
        at androidx.room.RoomTrackingLiveData$1.run(RoomTrackingLiveData.java:90)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
        at java.lang.Thread.run(Thread.java:764)

Room db ์ปฌ๋Ÿผ์„ nullable ํ•œ List<String>? ์„ ์ถ”๊ฐ€ํ•˜๊ณ , migration ํ…Œ์ด๋ธ” ์ถ”๊ฐ€ ๋ถ€๋ถ„

database.execSQL("ALTER TABLE 'memo' ADD COLUMN 'image' TEXT")

๐Ÿ˜‰  ์—๋Ÿฌ ํ•ด๊ฒฐ


์›๋ž˜ ์†Œ์Šค

@ProvidedTypeConverter
class StringListTypeConverter(private val gson: Gson) {

    @androidx.room.TypeConverter
    fun listToJson(value: List<String>?): String? {
        return value?.let{ gson.toJson(value) }
    }

    @androidx.room.TypeConverter
    fun jsonToList(value: String?): List<String> {
        return if (value?.isEmpty() == true) { // "" ๊ณต๋ฐฑ๊ฐ’
            Log.d("MemoDebug", "value==$value==")
            emptyList()
        } else {
            gson.fromJson(value, Array<String>::class.java).toList()
        }
    }
}

return๋ฌธ ๋ณด๋ฉด value?.isEmpty() ๋Š” "" ์„ ์–ธ์ด ๋œ value ๊ฐ’์ด ์—†๋‹ค๋Š” ๋œป์ด๋‹ค.

NULL ๊ณผ๋Š” ๋‹ค๋ฅด๋‹ค. NULL์€ ์•„์˜ˆ ์„ ์–ธ์กฐ์ฐจ ๋˜์ง€ ์•Š์€ ๊ฒƒ

๋‚˜๋Š” ๋‹น์—ฐํžˆ nullable ํ•œ coulmn์„ ๋งŒ๋“ค๊ณ  insert ํ–ˆ์œผ๋‹ˆ TypeConverter ๊ฐ€ null ๊ฐ’์„ ์–ด์ผ€ ๋ณ€ํ™˜ํ•ด์•ผ ํ• ์ง€ ๋ชฐ๋ผ 

else ๋ฌธ์„ ํƒ€๊ณ  NULL ์—๋Ÿฌ๋ฅผ ๋ฟœ์—ˆ๋‹ค.

 

 

์ˆ˜์ • ์†Œ์Šค

@androidx.room.TypeConverter
    fun jsonToList(value: String?): List<String> {
        return try{
            gson.fromJson(value, Array<String>::class.java).toList()
        }catch (e: Exception){
            emptyList()
        }
    }

try catch ๋กœ NULL ์žก์•„์ฃผ๊ธฐ

NULL ๊ฐ’์ด ์•„๋‹ˆ๋ฉด ๋ณ€ํ™˜ํ•˜๊ณ  NULL ๊ฐ’์ด๋ฉด ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ์žก์•„์คฌ๋”๋‹ˆ

๋“œ๋””์–ด Nullableํ•œ coulmn๊ฐ’ ์ถ”๊ฐ€ ์„ฑ๊ณต! ๐Ÿ˜ญ