1

I can't use the + operator with resultado[0] + obj.nopersonas, nopersonas is an Integer.

fhinicio(blank:false, validator : { val, obj ->
    def diff = groovy.time.TimeCategory.minus(obj.fhfinal, val)
    def total = diff.hours*60 + diff.minutes
    if (total < 15){
        return "reserva.fhfinal.tiempo.min.label"
    } else {            
        if (total > 60) {
            return "reserva.fhfinal.tiempo.max.label"
        } else {
            def reserva = Reserva.createCriteria()
            def resultado = reserva.list() {
                or {
                    and {
                        ge('fhinicio', val)
                        le('fhinicio', obj.fhfinal)
                    }
                    and {
                        ge('fhfinal', val)
                        le('fhfinal', obj.fhfinal)
                    }
                }

                projections {
                    sum('nopersonas')
                }
            } 

            //this is not working   
            def aff = resultado[0] + obj.nopersonas

Cannot execute null+null. Stacktrace follows: Message: Cannot execute null+null

doelleri
  • 19,232
  • 5
  • 61
  • 65
  • Well it seems `resultado[0]` and `obj.nopersonas` both are `null`. Check by debugging or simply printing the values if that's the case. – Ashraf Purno Oct 06 '15 at 04:40

3 Answers3

0

You've got a couple of problems to resolve:

  1. The criteria query is returning null
  2. obj.nopersonas is null

The criteria query

To fix the criteria query, start without the projection:

def reserva = Reserva.createCriteria()
def resultado = reserva.list {
    or {
        and {
            ge('fhinicio', val)
            le('fhinicio', obj.fhfinal)
        }
        and {
            ge('fhfinal', val)
            le('fhfinal', obj.fhfinal)
        }
    }
}

Make sure you're getting the appropriate instances of Reserva. Then, add the projection and since you're expecting a single value, use the get() method instead of list().

def reserva = Reserva.createCriteria()
def resultado = reserva.get {
    or {
        and {
            ge('fhinicio', val)
            le('fhinicio', obj.fhfinal)
        }
        and {
            ge('fhfinal', val)
            le('fhfinal', obj.fhfinal)
        }
    }

    projections {
        sum('nopersonas')
    }
} 

def aff = resultado + obj.nopersonas

obj.nopersonas

Since obj.nopersonas is null, I'm assuming the property is nullable. If the property is supposed to be nullable, then you'll need to account for that in your validator.

Emmanuel Rosa
  • 9,697
  • 2
  • 14
  • 20
0

It is working.

Integer variable_name = Integer.valueOf(resultado[0].toString())

0

You can simply handle the null condition:

def aff = (resultado[0] ?: 0) + (obj.nopersonas ?: 0)
Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121