[SOLVED Could not invoke method: java.lang.Math.abs(int) on instance: null

Tags: #<Tag:0x00007f617e1f8dd0> #<Tag:0x00007f617e1f8d08> #<Tag:0x00007f617e1f8c18>

Hello
I have small engine that works with “rollershutter”. Im using this in my project for studies that imitate smart house. Anyway, I want to control the level of the blind. So I created item:

Dimmer roleta_procent "Opuszczanie automatyczne" <rollershutter>

then I create rule:

import java.lang.Math
val CzasPodnoszenia=2000
val ProcentRolety=0

rule "Roleta automatyczna"
when
    Item roleta_procent received command
then
    val ZadanyProcent=roleta_procent.state as DecimalType //Input Level
    val ProcentRolety=Math::abs(ZadanyProcent-ProcentRolety) //Calculating Real level
    val KierunekRolety=if(ZadanyProcent>ProcentRolety) UP else DOWN //Calculating direction
    val Czas=(ProcentRolety*CzasPodnoszenia)/100 //Calculating time of blind move
    if (KierunekRolety==UP){
        executeCommandLine("pigs w 5 0") //Wybor kierunku GORA pin 5 /direction
        executeCommandLine("pigs w 22 1") //Wlaczenie silnika pin 22 /turning on engine
        executeCommandLine("pigs p 6 35") //Ustawienie predkoscsi pin 6 wartosc 35 na 255 /engine speed
        Thread::sleep(Czas) //Podniesienie rolety do danego procentu /time of work
        executeCommandLine("pigs p 6 0") //Zatrzymanie rolety /engine stop
    }
    if(KierunekRolety==DOWN){
        executeCommandLine("pigs w 5 1") //Wybor kierunku GORA pin 5
        executeCommandLine("pigs w 22 1") //Wlaczenie silnika pin 22
        executeCommandLine("pigs p 6 35") //Ustawienie predkoscsi pin 6 wartosc 35 na 255
        Thread::sleep(Czas) //Podniesienie rolety do danego procentu
        executeCommandLine("pigs p 6 0") //Zatrzymanie rolety
    }
end

but the rule doesn’t work and I can see this error in logviewer:

I was inspired by those 2 threads: 1 and 2

something you are assuming has a value is null

ProcentRolety is 0 at the first run and I don’t know if I read ZadanyProcent correctly from dimmer roleta_procent as I use:

roleta_procent.state as DecimalType

ZadanyProcent is of type DecimalType. ProcentRolety is of type BigDecimal. The result of the subtraction is of type BigDecimal. Math::abs requires an int. So you must convert the BigDecimal to a primitive int. Luckily there is a method to do just that:

val ProcentRolety=Math::abs((ZadanyProcent-ProcentRolety).intValue)

Unfortunately, the Rules DSL presents almost all type problems like this as a null problem.

1 Like

Thank you