Optional.ofNullable()中的空指针异常

我试图将一组转换为Optional.OfNullable方法内的字符串,例如:

test.setAbc(Optional.ofNullable(rule.getSampleSet().toString()).orElse(null));

but if sampleSet is null it will give me a NullPointerException. can anyone tell me how to resolve this issue using .map method with Optional?

我知道一种通过预先检查可空性的传统方式:

if(rule.getSampeSet != null)

但是我很想知道我们能否一站式完成。