I want to get xml data from free.worldweatheronline.com
I tried in flollowing way:-
<fx:Script>
<![CDATA[
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
private var main_xml_url:String = "http://free.worldweatheronline.com/feed/weather.ashx?q="+cityName.text.toString()+"&format=xml&num_of_days=2&key=my_api_key"; // there I put my api key not showing here//
protected function weatherService_resultHandler(event:ResultEvent):void
{
// TODO Auto-generated method stub
var result_weather_data:Object = event.result;
cityNameData.text = data.request.query;
}
protected function weatherService_faultHandler(event:FaultEvent):void
{
// TODO Auto-generated method stub
cityNameData.text = event.message.toString();
}
]]>
</fx:Script>
<fx:Declarations>
<s:HTTPService id="weatherService"
url="main_xml_url"
resultFormat="object"
result="weatherService_resultHandler(event)"
fault="weatherService_faultHandler(event)"/>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:TextInput id="cityName" text="Hyderabad" x="10" y="10" width="219" fontWeight="bold" prompt="Enter city Name"/>
<s:Label id="cityNameData" x="108" y="109" width="202" fontSize="20" fontWeight="bold"/>
But there comes an error saying that:
Cannot access a property or method of a null object reference.
at private var main_xml_url:String
please do a favour for by solving it Thanks in advance.
The code above means that you are trying to retrieve and assign the value of
cityName.text.toString()at object initialization time.The display object
cityNamewhich is aTextInputwill not be added to the display at the time of assignment. Hence, you are getting the NPE.You can correct this as follows:
Also, you forgot to call the
send()on theweatherServiceobject.This means that your webservice call would not have been triggered anyway.
For more info on this matter, check out the LiveDocs