Java 如何使用方法调用类中的字符串。调用(-,)

Java 如何使用方法调用类中的字符串。调用(-,),java,parsing,Java,Parsing,这里我试图从另一个项目中获取uContainer对象。具有所有setter和getter的uContainer,其返回值是从属性文件设置的。类似于特定用户的用户属性。我正在使用从uContainer实例获取特定的方法值。但是在第四行,我的应用程序崩溃了 uContainer是UserContainer类的一个实例 getSingleResultListing也是UserContainer类中具有with getter和setter方法的布尔变量 代码如下所示 Method getUContain

这里我试图从另一个项目中获取
uContainer
对象。具有所有setter和getter的uContainer,其返回值是从
属性文件设置的。类似于特定用户的用户属性。我正在使用从uContainer实例获取特定的方法值。但是在第四行,我的应用程序崩溃了

uContainer是UserContainer类的一个实例

getSingleResultListing
也是
UserContainer
类中具有with getter和setter方法的布尔变量

代码如下所示

Method getUContainer = form.getClass().getMethod("getUserContainer", new Class[0]);
Object uContainerObj = (Object)getUContainer.invoke(form, new Object[0]);
Method getFlagValueMethod = uContainerObj.getClass().getMethod("getSingleResultListing", new Class[0]);
String flagValue = (String)getFlagValueMethod.invoke(uContainerObj, new Object[0]);
log.info(">>>flagValue: "+flagValue);
boolean singleListingFlag = Boolean.getBoolean(flagValue);
log.info(">>>singleListingFlag: "+singleListingFlag);
在调用uContainer对象的第四行中,我得到了一个错误


谢谢..

您正在将返回的对象强制转换为
字符串
,但您无法从该方法中获得
字符串。不能通过强制转换运算符将对象转换为字符串。如果需要字符串表示,请编写

String flagValue = getFlagValueMethod.invoke(uContainerObj, new Object[0]).toString();

非常非常感谢..您的代码运行得非常好。我没有想到要使用toString()方法。这对我帮助很大。