我正在尝试将两个 csv 读入我的 SQL Server。即使我允许可为空并且每次都检查 null,还是会得到
NullPointerException
。调试时,似乎每次 null 检查表达式的计算结果都是 FALSE 条件,即使字符串为 null 也是如此。
这是我的空检查的示例:
( row2.Acq_date.equals(null) || row2.Acq_date.equals("") ||
row2.Acq_date == null ) ? null : (int)
TalendDate.diffDate(TalendDate.getCurrentDate(),row2.Acq_date,"MM")
你不应该像这样进行空检查
row2.Acq_date.equals(null)
。row2.Acq_date == null
(您实际上已将其包含在测试中,仅在测试row2.Acq_date.equals(null)
之后才执行此操作为时已晚,因为那是抛出NullPointerException
的部分)。
正确的做法是:
( row2.Acq_date == null ? null : (int)
TalendDate.diffDate(TalendDate.getCurrentDate(),row2.Acq_date,"MM")
根据您的评论,
row2.Acq_date
是日期类型,您可以使用适当的日期格式直接从文件中读取。如果文件中的列为空(或包含空格),Talend 将返回空日期,该日期由上述测试处理。