hi,
I am a fresh person for groovy. here is the code I have written:
public class TestAction extends ActionBase {
private static final long serialVersionUID = 8763503930833969428L;
public void Test(){
JSONObject params = requestParams != null?
JSONObject.fromObject(requestParams): new JSONObject();
try {
ClassLoader parent = getClass().getClassLoader();
GroovyClassLoader loader = new
GroovyClassLoader(parent);
String classText = "class DynamicGroovyClass { def
methodMissing(String name, args) {println \"You called $name with $args\"
args.size() } }";
@SuppressWarnings("rawtypes")
Class groovyClass = loader.parseClass(classText);
GroovyObject groovyObject = (GroovyObject)
groovyClass.newInstance();
Object [] args = {"hello", "world", "feel", "happy"};
Object result = groovyObject.invokeMethod("run", args);
System.out.println("Received:" + result);
sendJsonFormattedData(params.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
when I run the code, it throws an exception:
You called run with [hello, world, feel, happy]
java.lang.NullPointerException: Cannot get property 'args' on null
object
at
org.codehaus.groovy.runtime.NullObject.getProperty(NullObject.java:60)
at
org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:172)
at
org.codehaus.groovy.runtime.callsite.NullCallSite.getProperty(NullCallSite.java:47)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:296)
at
DynamicGroovyClass.methodMissing(script1452504513039443064905.groovy:1)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at
org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaClassImpl.invokeMissingMethod(MetaClassImpl.java:936)
at
groovy.lang.MetaClassImpl.invokePropertyOrMissing(MetaClassImpl.java:1259)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1212)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1019)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:810)
at DynamicGroovyClass.invokeMethod(script1452504513039443064905.groovy)
at com.shanghe.action.TestAction.Test(TestAction.java:31)
so. why groovy can't get property args but it can print args? how to
resolve the exception?