Simulated switch is "null", cannot perform actions


I have this SmartApp that should turn on a simulated switch, but when i do “switch.on()” a NullPointerException is thrown, because switch is null.
Yet, the correct device and name are selected in the preferences. Let me link some code.

preferences {  //Set the sensor and the relay
    section("Choose devices"){
	input "sensor", "capability.temperatureMeasurement", required: true, title: "Sensore di temperatura"
        input "device", "capability.switch", required: true, title: "Relay"
//Cut out due to lenght, i have other 16 preferences...


if (temperature < workTime1Temperature) device?.on();
    else device?.off();  //Safe navigation to avoid exception"${device}");  //This prints "null"

Here are some screenshots from the SmartThings app.

I don’t get it. The device is configured correctly… selected in the list…
May the Virtual Device have an influence on this?

Thanks to everyone in advance

Please, it is nearly always more helpful to post a GitHub link to your FULL CODE if you want help debugging.

That way the helper can just copy/paste the SmartApp into their IDE and replicate the error and respond with just a couple edits!

If you suspect a Virtual Device issue, then test using a Physical Device!