Search code examples
ubuntugrailsdoublemultiplication

double number multiplication is not working in Grails, under ubuntu 14.04 server


I am multiplying double numbers in Grails 2.2.1 and saving it as the string.It is working fine under local environment(windows 7 , jdk 7, tomcat7) but not working under the ubuntu server (14.04, tomcat7 , jdk7).I am not able to find out the root cause behind it.

Here is the error log

public java.lang.Number java.lang.Number#multiply(java.lang.Number).Stacktrace follows:

org.codehaus.groovy.runtime.metaclass.MethodSelectionException: Could not find which method multiply() to invoke from this list:

  public java.lang.Number java.lang.Number#multiply(java.lang.Character)
  public java.lang.Number java.lang.Number#multiply(java.lang.Number)

    at sagoon.ProductsController.addDiamondProduct(ProductsController.groovy:232)

    at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:195)

    at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)

    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)

    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)

    at java.lang.Thread.run(Thread.java:745)

code snippest

 def today = new Date().clearTime()
    def todayGoldPrice = GoldRates.findAllByGoldDate(today)
    def category = Categories.get(params.category)
    def karigad = Karigads.get(params.karigad)

    Double totalDiamondAmount = params.diamondWeight.toDouble() * params.diamondRate.toDouble()

    Double totalGoldAmount = params.goldWeight.toDouble() * ( params.goldPercent.toDouble()/100 )  * todayGoldPrice.rate

    Double totalMc = params.goldWeight.toDouble() * params.mcPerGram.toDouble()

    Double totalStoneAmount = params.stoneWeight.toDouble() * params.stonePricePerCarat.toDouble()

    Double totalPurchasePrice = totalDiamondAmount.toDouble() + totalGoldAmount.toDouble() + totalStoneAmount.toDouble() + totalMc.toDouble()

ProductsController.addDiamondProduct

 def addDiamondProduct(){

    def today = new Date().clearTime()
    def todayGoldPrice = GoldRates.findAllByGoldDate(today)
    def category = Categories.get(params.category)
    def karigad = Karigads.get(params.karigad)

    Double totalDiamondAmount = params.diamondWeight.toDouble() * params.diamondRate.toDouble()
    Double totalGoldAmount = params.goldWeight.toDouble() * ( params.goldPercent.toDouble()/100 )  * todayGoldPrice.rate
    Double totalMc = params.goldWeight.toDouble() * params.mcPerGram.toDouble()
    Double totalStoneAmount = params.stoneWeight.toDouble() * params.stonePricePerCarat.toDouble()
    Double totalPurchasePrice = totalDiamondAmount.toDouble() + totalGoldAmount.toDouble() + totalStoneAmount.toDouble() + totalMc.toDouble()

    def product = new DiamondProducts(
            productCode: params.productCode,
            diamondWeight: params.diamondWeight,
            diamondRate: params.diamondRate,
            totalDiamondAmount: totalDiamondAmount.toString(),
            goldWeight: params.goldWeight,
            goldKt: params.goldPurity,
            goldPercent: params.goldPercent,
            totalGoldAmount: totalGoldAmount.toString(),
            mcGram: params.mcPerGram,
            totalMc: totalMc.toString(),
            stoneWeight: params.stoneWeight,
            stonePricePerCarat: params.stonePricePerCarat,
            totalStoneAmount: totalStoneAmount.toString(),
            totalPurchasePrice: totalPurchasePrice.toString(),
            salePrice: params.salePrice,
            remarks: params.remarks,
            description: params.description,
            itemNumber: params.itemNumber,
            karigad: karigad,
            category: category,
            status: "notSold",
            createdDate: new Date()
    )
    product.save()

    flash.message = "Product Added Successfully."
    redirect(action: 'diamondProducts')

}

What I am doing wrong here?


Solution

  • I don't think so that it's giving you this error due to different working environment.I have tested it on the Ubuntu 14.04 environment and it is working just fine.

    You can see in your error log that, at the run-time groovy engine is getting confused with the multiply method.It is not able to select the any one of the method as there are two options available and that's why it's giving MethodSelectionException.

    public java.lang.Number java.lang.Number#multiply(java.lang.Character)
    public java.lang.Number java.lang.Number#multiply(java.lang.Number)
    

    Also please have a look at below lines in the code snippet added by you

    def todayGoldPrice = GoldRates.findAllByGoldDate(today)
    .
    .
    .
    Double totalGoldAmount = params.goldWeight.toDouble() * ( params.goldPercent.toDouble()/100 )  * todayGoldPrice.rate
    

    Here in the the first line you have done findAllByGoldDate query on the Domain class GoldRates. The findAllByGoldDate will return the list of objects matching the query criteria.

    In the another line where you are doing the multiplication, look at the way you are accessing the rate field from the todayGoldPrice collection which has list of objects.I recommend you to replace findAllBy with findBy if you want to access the rate field directly like todayGoldPrice.rate which you have done in your code.

    Please look into it and if possible use the same database dump in both the environment.