gpt4 book ai didi

groovy - Groovy歧义方法重载

转载 作者:行者123 更新时间:2023-12-03 22:31:15 29 4
gpt4 key购买 nike

早上好-我看到有类似的问题问过几次,但它们似乎都是针对已编译项目或涉及Gradle的项目。无论如何,我遇到了错误

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]


当我运行这个小脚本

String amt = "1"
String currency = "GBP"
String targetCurrency = "USD"

def settlement = crossCurrencyClient.crossCurrency(amt, currency, targetCurrency)

return transfer.amount * new java.math.BigDecimal (settlement)


这本身触发了

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()


我无法解决问题所在。据我所知,所有方法均正确完成,没有任何歧义。谁能指出我要去哪里了?

最佳答案

模棱两可的方法调用是BigDecimal的构造函数:

Ambiguous method overloading for method java.math.BigDecimal#<init>


进一步说,一个可能的重载是 BigDecimal(String val)构造函数。我不确定 [class [C]到底指的是什么,但是我猜想它是指 BigDecimal(BigInteger val)的。

导致它的行可能是这一行:

new java.math.BigDecimal (settlement)


因为结算变量为null。在类似情况下,您可以像这样强制转换参数:

new java.math.BigDecimal (settlement as String)


但稍后可能会引发NullPointerException。因此,只需确保不将null传递给BigDecimal的构造函数即可。

关于groovy - Groovy歧义方法重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33410033/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com