jira – No such property error for String in Groovy

jira – No such property error for String in Groovy

Because you are using this code as an script. Upon compilation, all code not contained in methods will be put in the run() method, thus, your errorString will be declared in the run() method, and your mainMethod() will try to work with an errorString which doesnt exist neither in its own scope nor in the class scope.

Some solutions:

1. @Field

Adding @Field turns your errorString into a field in the compiled script class

@groovy.transform.Field String errorString = 

def mainMethod(){
    if (errorString.length()) {
        errorString += BTW, fields with errors must be either corrected or set back to their original value before proceeding.
        invalidInputException = [error: errorString]
    }
}

mainMethod()

2. Binding

Remove the type declaration, so errorString will be contained in the scripts binding:

errorString = 

def mainMethod(){
    if (errorString.length()) {
        errorString += BTW, fields with errors must be either corrected or set back to their original value before proceeding.
        invalidInputException = [error: errorString]
    }
}

mainMethod()

Here is groovyConsole output of your script:

ast

You have to add @Field annotation to errorString:

import java.lang.String
import groovy.transform.Field
//More imports ...

@Field
String errorString = 
//More global variables

def mainMethod(){
    if (errorString.length()) {
        errorString += BTW, fields with errors must be either corrected or set back to their original value before proceeding.
        invalidInputException = new     InvalidInputException(errorString);
    }
}

jira – No such property error for String in Groovy

Leave a Reply

Your email address will not be published. Required fields are marked *