简体   繁体   English

为什么groovy会出现MissingMethodException的错误?

[英]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.

相关问题 Groovy类重写了构造函数,为什么会缺少MissingMethodException? - Groovy class overridden constructor, why MissingMethodException? 错误:groovy.lang.MissingMethodException:没有方法签名 - ERROR:groovy.lang.MissingMethodException: No signature of method groovy.lang.MissingMethodException:没有方法签名:为什么 Jenkins 共享管道库会出现此错误? - groovy.lang.MissingMethodException: No signature of method: Why is this error coming for Jenkins Shared Pipeline library? Jenkins Groovy构建作业错误groovy.lang.MissingMethodException - Jenkins groovy build job error groovy.lang.MissingMethodException 为什么我无法捕获Groovy的MissingMethodException? - Why Can't I catch Groovy's MissingMethodException? MissingMethodException:使用Groovy元类替换方法后,方法错误没有签名吗? - MissingMethodException: No signature of method error after replacing method using groovy metaclass? Groovy中的MissingMethodException:没有方法签名 - MissingMethodException in Groovy: No signature of method Groovy中缺少静态类的MissingMethodException - MissingMethodException in groovy for a static class Groovy脚本中的MissingMethodException - MissingMethodException in groovy scripting 尝试使用 readFile() 时 Jenkins Groovy 错误 groovy.lang.MissingMethodException - Jenkins Groovy error groovy.lang.MissingMethodException when trying to use readFile()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM