Почему возникает ошибка MissingMethodException в groovy? - PullRequest
0 голосов
/ 03 февраля 2020
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)
    } 
}

Есть сообщение об ошибке,

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)

Я проверил его на https://www.tutorialspoint.com/execute_groovy_online.php

Спасибо всем, когда пытался выполнить код , столкнулся ниже ошибка.

Condition not satisfied:
updateGlobalInteger(errorC)
|                   |
0                   0

А

java.lang.NullPointerException: Cannot invoke method size() on null object
at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:91)

1 Ответ

1 голос
/ 03 февраля 2020

В сообщении об ошибке говорится, что вы не можете получить доступ к методу non stati c непосредственно в области stati c, что четко указано в java документах

Caught : groovy .lang.MissingMethodException: Нет сигнатуры метода: статический Example.updateGlobalInteger ()

Методы класса не могут напрямую обращаться к переменным экземпляра или методам экземпляра - они должны использовать ссылку на объект. Кроме того, методы класса не могут использовать ключевое слово this, поскольку для этого нет экземпляра, на который можно ссылаться.

Так что либо сделайте этот метод stati c

static Integer updateGlobalInteger(Integer errorC) {
    errorCount=errorC
   return errorCount
}

Или используя ссылка на объект

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)
} 
...