我把 CSV 导出循环从 Java 迁到 Groovy,现在遇到 nickname 缺失的那一行,任务就挂了。Java 的 FieldEncoder 有三个 encode 重载(String、Number、Collection),null 应该变成标记 NULL。以前的 Java 代码里 encoder.encode(user.getNickname()) 传 null 完全没问题。
Groovy 这边的代码如下。
def cells = COLUMNS.collect { col -> encoder.encode(row[col]) }
第 3 行没有 nickname 这个 key。报错第一行说方法重载有歧义,参数列表是 [null]。我把 row[col] 打印出来,显示的是 null,所以 map 看起来正常。我还改了 nullToken,没有区别。
是 Groovy 4.0.22 对缺失的 key 返回了什么奇怪的东西,还是不支持 null 标记这个选项?为什么同样是 null,Java 能处理,Groovy 就不行?
谢谢大家帮忙。
从英语翻译·查看原文I moved my CSV export loop from Java to Groovy and now the job dies on the row with missing nickname. Java FieldEncoder has three encode overloads (String, Number, Collection), and null should become token NULL. In old Java code encoder.encode(user.getNickname()) works fine with null.
Groovy side:
def cells = COLUMNS.collect { col -> encoder.encode(row[col]) }
Row 3 has no nickname key. First line of the error say ambiguous method overloading, and the argument list is [null]. I print row[col] and it show null, so map looks normal. I also changed nullToken, no difference.
Is Groovy 4.0.22 returning something strange for missing key, or not supporting the null token option? Why Java handle null here but Groovy cannot?
Thanks for any help.