如何以null为参数反射调用方法? [英] How do I reflectively invoke a method with null as argument?

查看:738
本文介绍了如何以null为参数反射调用方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图反过来用Java调用这个方法:

I am trying to invoke this method in Java reflectively:

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

问题是我想传递null为 null ,这样foo就变成了空。

The problem is that I want to pass null as null, so that foo becomes null.

但是,在下面的方法中假设没有参数,我得到 IllegalArgumentException 错误的参数数量):

However, in the following approach it assumes that there are no arguments, and I get IllegalArgumentException(wrong number of arguments):

method.invoke(new FooHolder(), null);
// -----------------------------^ - I want null to be passed to the method...

这是如何实现的?

推荐答案

尝试

method.invoke(new FooHolder(), new Object[]{ null });

这篇关于如何以null为参数反射调用方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆