Groovy模糊方法重载
早上好——我看到类似的问题已经被问过好几次了,但它们似乎都是针对编译过的项目或涉及Gradle的项目。不管怎样,我得到了错误Groovy模糊方法重载,groovy,Groovy,早上好——我看到类似的问题已经被问过好几次了,但它们似乎都是针对编译过的项目或涉及Gradle的项目。不管怎样,我得到了错误 Caused by: javax.script.ScriptException: groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method java.math.BigDecimal#<init>. Cannot resolve which method to invo
Caused by: javax.script.ScriptException: groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method java.math.BigDecimal#<init>.
Cannot resolve which method to invoke for [null] due to overlapping prototypes between:
[class [C]
[class java.lang.String]
这本身就触发了这一点
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.RESTClient
import groovy.json.JsonBuilder
import groovy.json.JsonOutput
import static groovyx.net.http.Method.GET
import static groovyx.net.http.ContentType.JSON
public class CrossCurrencyClient {
def issuingAddress = "rBycsjqxD8RVZP5zrrndiVtJwht7Z457A8"
String source = "rUR5QVHqFxRa8TSQawc1M6jKj7BvKzbHek"
String multiplier = ""
def resURL = "http://url-string.com/v1/"
def resourceIdClient = new RESTClient("${resURL}")
public String generateUUID() {
def resourceId = resourceIdClient.get(path:"uuid").data.uuid
println "resourceId = " + resourceId
return resourceId
}
public String crossCurrency(String amt,String currency,String targetCurrency) {
def http = new HTTPBuilder( "${resURL}accounts/${source}/payments/paths/${source}/${amt}+${targetCurrency}+${issuingAddress}?source_currencies=${currency}+${issuingAddress}"
)
http.request(GET,JSON) {
response.success = { resp, json ->
if(json.success){
multiplier = json?.source_amount?.value
}
}
response.failure = { resp ->
println "Request failed with status ${resp.status} and message : ${resp.message}"
return "Something went wrong"
}
}
return multiplier
}
}
CrossCurrencyClient crossCurrencyClient = new CrossCurrencyClient()
我想不出这里有什么问题。据我所知,所有的方法都做得很好,没有歧义。有人能指出我错在哪里吗?正如例外情况所说,
结算
是null
,因此Groovy不知道调用biginger
的哪个构造函数(因为null可以是其中之一)
它是空的,因为:
if(json.success){
multiplier = json?.source_amount?.value
}
因此,如果json
或source\u amount
为空,则乘数
为空,因此返回空
您可以停止返回null的方法。。。或将构造函数更改为:
return transfer.amount * new BigDecimal((String)settlement)
不明确的方法调用是BigDecimal的构造函数:
Ambiguous method overloading for method java.math.BigDecimal#<init>
因为结算变量为空。在类似情况下,可以按如下方式强制转换参数:
new java.math.BigDecimal (settlement as String)
但稍后它可能会抛出NullPointerException。因此,请确保不要将空值传递给BigDecimal的构造函数。由于BigDecimal有多个构造函数,我假设我无法确定采用哪一个构造函数,因此出现了错误谢谢。正如您正确建议的那样,它抛出了一个NullPointerException,但这很容易解决。
new java.math.BigDecimal (settlement)
new java.math.BigDecimal (settlement as String)