How do I reflectively invoke a method with null as argument?

后端 未结 4 1033
臣服心动
臣服心动 2021-02-02 07:32

I am trying to invoke this method in Java reflectively:

public void setFoo(ArrayList foo) { this.foo = foo; }

The problem is that

4条回答
  •  没有蜡笔的小新
    2021-02-02 08:30

    The compiler warning should make you aware of the problem;

    The argument of type null should explicitly be cast to Object[] for the invocation of the varargs method invoke(Object, Object...) from type Method. It could alternatively be cast to Object for a varargs invocation

    You can fix it like this;

    Object arg = null;
    method.invoke(new FooHolder(), arg);
    

提交回复
热议问题