Complete
over 4 years ago

Not a bug.

Weather.getCurrentConditions().attribute crashes when data is not available SDK 3.2.1

I am using the brand new shiny SDK 3.2.1 weather feature which I absolutely love. However, I was dismayed when my watch face crashed in the simulator after I unchecked the "Weather Data Available" box in the settings. I was calling Weather.getCurrentConditions().temperature, and I got an unexpected type value error. When I called Weather.getCurrentConditions() instead, it recognized it as a null value and I could use an if statement to work around it. I have not tested the other weather functions or attributes.

Can the attributes for the weather functions get null values when data is not available? Thanks

Former Member
Former Member
Parents
  • Former Member
    Former Member over 4 years ago in reply to jim_m_58

    Hi Jim,

    What you're doing there is similar to my workaround, although I am not following your use of makeTemp.

    Here's my full function:

    	static function getTemperature() {
    		
    		var value;		
    		var degreeSign = StringUtil.utf8ArrayToString([0xC2,0xB0]);
    		var units = degreeSign + Ui.loadResource(Rez.Strings.degC);
    		var unitsNum = Sys.getDeviceSettings().temperatureUnits;
    		
    		if (Weather.getCurrentConditions() == null) {
    			value = "-- ";
    			
    			if (unitsNum == 1) {
    				units = degreeSign + Ui.loadResource(Rez.Strings.degF);
    			}
    		}
    		else {
    			value = Weather.getCurrentConditions().temperature;  // units are degC
    
    			if (unitsNum == 1) {
    				value = ((value * (9.0d / 5.0d)) + 32).toNumber();
    				units = degreeSign + Ui.loadResource(Rez.Strings.degF);
    			}
    		}
    		
    		var reading = value + units;
    		
    		return reading;
    	}

    The app crashes when I try this:

    if (Weather.getCurrentConditions().temperature == null) {
    
    // do something
    
    }

    I'm relatively new at coding and Connect IQ. Is this behavior normal for attributes?

Comment
  • Former Member
    Former Member over 4 years ago in reply to jim_m_58

    Hi Jim,

    What you're doing there is similar to my workaround, although I am not following your use of makeTemp.

    Here's my full function:

    	static function getTemperature() {
    		
    		var value;		
    		var degreeSign = StringUtil.utf8ArrayToString([0xC2,0xB0]);
    		var units = degreeSign + Ui.loadResource(Rez.Strings.degC);
    		var unitsNum = Sys.getDeviceSettings().temperatureUnits;
    		
    		if (Weather.getCurrentConditions() == null) {
    			value = "-- ";
    			
    			if (unitsNum == 1) {
    				units = degreeSign + Ui.loadResource(Rez.Strings.degF);
    			}
    		}
    		else {
    			value = Weather.getCurrentConditions().temperature;  // units are degC
    
    			if (unitsNum == 1) {
    				value = ((value * (9.0d / 5.0d)) + 32).toNumber();
    				units = degreeSign + Ui.loadResource(Rez.Strings.degF);
    			}
    		}
    		
    		var reading = value + units;
    		
    		return reading;
    	}

    The app crashes when I try this:

    if (Weather.getCurrentConditions().temperature == null) {
    
    // do something
    
    }

    I'm relatively new at coding and Connect IQ. Is this behavior normal for attributes?

Children
  • "although I am not following your use of makeTemp."

    makeTemp is just a helper function that does things like C to F conversion. 

  • Former Member
    Former Member over 4 years ago in reply to jim_m_58

    Hi Jim,

    Thanks for the tip. I have this function in a separate Util.mc file, but in this case the strings are so simple I can just use "C" and "F" instead of calling resources.

    Since temperature is the only thing I need, at first I did not even think about using the broader function Weather.getCurrentConditions() by itself. But after reading the API and seeing that it should return null, I decided to try it.

    This is the error I get in the console:

    Error: Unexpected Type Error
    Details: Failed invoking <symbol>
    Stack:
    - getTemperature() at C:\...\source\Util.mc:211 0x1000071d

    - ...

    - ...

    After I this code: 

    The line reference is the same line as the if statement.

  • Just a general comment, but some of the stuff you're doing each time the function is called can just be done once when the app starts. No need to do the loadResource each time for example.  That's something that hits the file system, so can be expensive.

  • "temp" in my code is just short for temperature.  It's the sting I'll be displaying.

    You shouldn't need to null check Weather.getCurrentConditions().temperature

    What is the error you see, and which line is referenced?