Fabrizio Fortino
Fabrizio Fortino

Reputation: 1606

Groovy: unable to set properties from java

I am developing a Grails application. I have a Groovy bean with a few properties. I am trying to instantiate and set some of the properties from a Java class using the setter methods. Although I don't have any error the properties don't contain any value. I tried to look at the object content in debug mode: the groovy object contains 1 unique field (r$fields) which contains an empty HashMap.

Groovy Bean

import java.time.LocalDateTime;

import groovy.transform.ToString;

@ToString(includeNames = true, ignoreNulls = true)
class Article {

    String id
    String modelId
    String ean
    String deepUrl

    LocalDateTime lastUpdate
}

Java code

a = new Article();
a.setId("123");
a.setModelId("456");
a.setEan("789");
a.setLastUpdate(LocalDateTime.now(Clock.systemUTC()));

After executing the above piece of code the object a results empty.enter image description here

Adding a.toString() as last statement in the Java code I get the following NPE

Stacktrace follows: java.lang.NullPointerException at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:39) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)

Upvotes: 1

Views: 349

Answers (1)

Fabrizio Fortino
Fabrizio Fortino

Reputation: 1606

I found a solution for this issue but I don't really understand the reason of that. The problem is related to the variable deepUrl. When removed the code works as a charm. I tested it on the groovy web console and it works *https://groovyconsole.appspot.com/script/5656619568332800). Can this be related to grails?

Upvotes: 2

Related Questions