Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy ambiguous method overload

Tags:

Morning all - I see that similar questions have been asked a couple of times, but they all seem to be for compiled projects or those involving Gradle. Anyway, I'm getting the error

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]

when I run this little script

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

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

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

which itself triggers this

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

I can't work out what the problem is here. As far as I can see all the methods are done properly and there is no ambiguity. Can anyone point to where I'm going wrong?

like image 833
Horsebrass Avatar asked Oct 29 '15 09:10

Horsebrass


Video Answer


1 Answers

The ambiguous method call is the BigDecimal's constructor:

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

Further it says that one possible overload is the BigDecimal(String val) constructor. I am not sure what exactly does [class [C] refer to but I would guess it refers to the BigDecimal(BigInteger val).

The line that's causing it is probably this one:

new java.math.BigDecimal (settlement)

because the settlement variable is null. In similar situations you could just cast the parameter like so:

new java.math.BigDecimal (settlement as String)

but it will probably throw NullPointerException later. So just make sure you don't pass nulls to BigDecimal's constructor.

like image 141
Michal M Avatar answered Sep 18 '22 08:09

Michal M