I have a simple app that I wrote myself to give me weather alerts. It has worked fine. Based on things I have been reading I wanted to change the httpget to asynchttpget. Having a little issue with the response. Not being that good a programmer I don't know what to do here. So looking for some help.
Here is the original code:
def getAlerts(evt){
wxURI = "https://api.weather.gov/alerts?active=true&message_type=alert&point=36.67%2C-93.34&code=SVA,SVR,TOA,TOR"
def requestParams =
[
uri: wxURI,
requestContentType: "application/json",
contentType: "application/json"
]
httpGet(requestParams) { response ->
if (response?.status == 200){
if(response.data.features){
log.debug "Building alertmsg."
// build out variables from JSON feed
alertheadline = response.data.features[0].properties.headline
event = response.data.features[0].properties.event
effective = response.data.features[0].properties.effective
ends = response.data.features[0].properties.ends
msg = alertheadline.replaceAll("by NWS Springfield MO","")
msg = msg.replaceAll("CDT","")
msg = msg.replaceAll("CST","")
id = response.data.features[0].properties.id
}
else {msg = "No Current Weather Alerts"
id = "none"
log.debug "${msg}"}
}
else log.warn "${response?.status}"
}
}
Here is the new code. I get the following error .
The error I get is: errorjava.lang.NullPointerException: Cannot get property 'features' on null object on line 87 (alertHandler)
Line 87 is the one if(response.data.features){
def getAlerts(evt){
wxURI = "https://api.weather.gov/alerts?active=true&message_type=alert&point=36.67%2C-93.34&code=SVA,SVR,TOA,TOR"
def requestParams =
[
uri: wxURI,
requestContentType: "application/json",
contentType: "application/json"
]
asynchttpGet("alertHandler",requestParams)
}
def alertHandler(resp, data) {
if(resp.getStatus() == 200 || resp.getStatus() == 207) {
response = parseJson(resp.data)
if(response.data.features){
log.debug "Building alertmsg."
// build out variables from JSON feed
alertheadline = response.data.features[0].properties.headline
event = response.data.features[0].properties.event
effective = response.data.features[0].properties.effective
ends = response.data.features[0].properties.ends
msg = alertheadline.replaceAll("by NWS Springfield MO","")
msg = msg.replaceAll("CDT","")
msg = msg.replaceAll("CST","")
id = response.data.features[0].properties.id
}
else {msg = "No Current Weather Alerts"
id = "none"
log.debug "${msg}"}
else log.warn "${response?.status}"
}