0
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

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)
cfrick
  • 35,203
  • 6
  • 56
  • 68
Ju3332
  • 33
  • 4
  • u can't use a non-static method without creating the object. https://stackoverflow.com/questions/290884/what-is-the-reason-behind-non-static-method-cannot-be-referenced-from-a-static – Bhanuchander Udhayakumar Feb 03 '20 at 05:05
  • `contain` is `null`, because the string "ERROR" was not found – cfrick Feb 03 '20 at 11:19
  • Also if you just want a string of all the `log` entries, just use `.join(", ")` - the `error` is build up is quite convoluted – cfrick Feb 03 '20 at 11:20

1 Answers1

1

The error message says you cannot access non static method in static area directly, and that is specified clearly in java docs

Caught: groovy.lang.MissingMethodException: No signature of method: static 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.

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)
} 
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
  • Thanks :) I just tried your method, no previous error. But got below error when running in the iIntelliJ Condition not satisfied: updateGlobalInteger(errorC) | | 0 0 Any advice for this error? Thanks :) – Ju3332 Feb 03 '20 at 05:53
  • Maybe I can try if it return to null, then set it to int 0 – Ju3332 Feb 03 '20 at 06:44