Accessing Weather.CurrentConditions.observationLocationPosition causes app crash on some devices

I'm struggling with crashes on seemingly random devices of this simple code:

if (Toybox has :Weather) {
	if (Toybox.Weather has :getCurrentConditions) {
		var w = Weather.getCurrentConditions();
		if (Toybox.Weather.CurrentConditions has :observationLocationPosition) {
			if (w.observationLocationPosition != null) {
				lastLocation = w.observationLocationPosition;
			}
		}
	}
}

It work on 99% of devices, but a few users get the following error:

Error: Unexpected Type Error
Details: 'Failed invoking <symbol>'
Time: 2021-11-01T13:29:19Z
Part-Number: 006-B3291-00
Firmware-Version: '19.20'
Language-Code: dut
ConnectIQ-Version: 4.0.6

It fails at these lines (some error code points to if, some to ne 2nd line):

if (w.observationLocationPosition != null) {
  lastLocation = w.observationLocationPosition;
}

I already ckeck all "has" conditions I could think of, and still it fails for some users. It works on 2 of my watches and in all simulators - it is real devices that have a problem. Any way to avoid it? Thanks!

Top Replies

  • As per the docs, Weather.getCurrentConditions() can return null, so you need to check w for null in your code. From my reading (between the lines) of the docs, it seems that you don't need all of those…

All Replies