Search code examples
javagroovy

groovy.lang.MissingMethodException: No signature of method: java.lang.String.name() is applicable for argument types: () values: []


I am new to this groovy script, as i have requirement to update the java 1.7 to java8 for my project,after upgrading the version both java & groovy then facing issue with the groovy syntax. In java 7 it worked without any issues,but in java 8 facing below error .

The below error is occruing while i am trying to find specific tag element as below :

def tradeString = msg."**".find{it.name() == "m__tradeString"}
groovy.lang.MissingMethodException: No signature of method: java.lang.String.name() is applicable for argument types: () values: []
Possible solutions: take(int), take(int), any(), any(groovy.lang.Closure), wait(), dump()

Note : in some places name() method is working but other places facing this error

please help me here.

Thanks in advance.


Solution

  • After discussion in comments..

    For now I see you iterating through the collection with groovy.util.Node objects, and calling it.name() seems valid for them.

    But from the initial error message in your question I saw the String object too. So you might have at least 2 different types (String and Node). And you have to handle this.

    def tradeString = msg."**".find{
        String expected = 'm__tradeString'
        String actual = it instanceof groovy.util.Node ? it.name() : it
        actual == expected
    }
    

    This code can be shortened of course.

    And for such cases I suggest to look throw the initial collection in debugger, or print each object type .getClass() and .toString(). So It'll become more clear.

    groovy.lang.MissingMethodException: No signature of method: java.lang.String.name() is applicable for argument types: () values: []

    Means that method name() was invoked on java.lang.String, but this class doesn't have such method.


    Less code version:

    // this method can be reused
    static String extractNodeName(node) {
        node instanceof groovy.util.Node ? node.name() : node
    }
    
    def tradeString = msg."**".find{ extractNodeName(it) =='m__tradeString' }