[英]Why there is an error of MissingMethodException in groovy?
class Example {
static Integer errorCount
static Integer updateGlobalInteger(Integer errorC){
errorCount=errorC
return errorCount
}
static void main(String[] args) {
List<String> log = ["ABCD","WARNING"]
println(log)
def error = log.toString().replace("]","").replace("[","")
println(error)
def contain = error.find("ERROR") //if it is null, then error occur
println(contain)
Integer errorC = contain.size()
println(errorC)
updateGlobalInteger(errorC)
}
}
There is a error msg,有错误消息,
Caught: groovy.lang.MissingMethodException: No signature of method: static
Example.updateGlobalInteger() is applicable for argument types: (java.lang.Integer) values: [10]
Possible solutions: updateGlobalInteger(java.lang.Integer)
groovy.lang.MissingMethodException: No signature of method: static Example.updateGlobalInteger() is
applicable for argument types: (java.lang.Integer) values: [10]
Possible solutions: updateGlobalInteger(java.lang.Integer)
at Example.main(main.groovy:14)
I tested it on https://www.tutorialspoint.com/execute_groovy_online.php我在https://www.tutorialspoint.com/execute_groovy_online.php上对其进行了测试
Thanks all, when I tried to execute the code, faced below error.谢谢大家,当我尝试执行代码时,遇到以下错误。
Condition not satisfied:
updateGlobalInteger(errorC)
| |
0 0
And和
java.lang.NullPointerException: Cannot invoke method size() on null object
at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:91)
The error message says you cannot access non static method in static area directly, and that is specified clearly in java docs错误信息说您不能直接访问静态区域中的非静态方法,这在 java docs 中有明确说明
Caught: groovy.lang.MissingMethodException: No signature of method: static Example.updateGlobalInteger()
捕获:groovy.lang.MissingMethodException:没有方法签名:静态 Example.updateGlobalInteger()
Class methods cannot access instance variables or instance methods directly—they must use an object reference.类方法不能直接访问实例变量或实例方法——它们必须使用对象引用。 Also, class methods cannot use the this keyword as there is no instance for this to refer to.
此外,类方法不能使用 this 关键字,因为没有可供 this 引用的实例。
So either make that method static所以要么使该方法静态
static Integer updateGlobalInteger(Integer errorC) {
errorCount=errorC
return errorCount
}
Or by using object reference或者通过使用对象引用
static void main(String[] args) {
// Example of an Integer using def
def rint = 1..10
Integer errorC = rint.size().toInteger()
println(errorC) //print result is 10
Example e = new Example()
e.updateGlobalInteger(errorC)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.