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
  • 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?

  • Not sure I understand.  Are you null checking what you get when you call getCurrentConditions()?  Just tied unchecking "weather data available with this code, and I get "No Conditions" displayed

        	var temp="No Weather";
        	if(hasWeather) {
        		temp="No Conditions";
        		var cc=Weather.getCurrentConditions();
        		if(cc!=null) {
        			temp=makeTemp(cc.temperature).format("%0.1f")+degree;
        		}
        	}
        	
        	//display the temp variable