简体   繁体   English

Groovy:无法从Java设置属性

[英]Groovy: unable to set properties from java

I am developing a Grails application. 我正在开发Grails应用程序。 I have a Groovy bean with a few properties. 我有一个具有一些特性的Groovy bean。 I am trying to instantiate and set some of the properties from a Java class using the setter methods. 我正在尝试使用setter方法实例化并设置Java类中的某些属性。 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对象包含1个唯一字段(r $ fields),其中包含一个空的HashMap。

Groovy Bean 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 Java代码

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. 执行完以上代码后,对象的结果为空。 在此处输入图片说明

Adding a.toString() as last statement in the Java code I get the following NPE a.toString()作为Java代码中的最后一条语句添加,我得到以下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) 堆栈跟踪如下:org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:39处的java.lang.NullPointerException org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)在org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)在org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)

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. 问题与变量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 ). 我在groovy Web控制台上对其进行了测试,并且可以正常工作* https://groovyconsole.appspot.com/script/5656619568332800 )。 Can this be related to grails? 这可能与grails有关吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM