Java 8 method reference to class instance method NPE

前端 未结 2 1148
清酒与你
清酒与你 2021-01-29 15:45
import java.util.function.Function;

public class Playground {
    public static void main (String[] args) {
        Object o = null;
        System.out.println(o);
             


        
2条回答
  •  一生所求
    2021-01-29 16:35

    when you do

    toStringFunc.apply(o);
    

    this is the same that

    o.toString()
    

    that's why you get a NullPointerException so, you must ensure that your Object is not null.

提交回复
热议问题