Need Help With Code Modification

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}"
}

Never Mind. Figured it out.

1 Like

Can you post how you did it please?

Where the problem was, I had too many 'data'. when I did a parseJson(resp.data) it parsed out the data result. So where I worked on the data I had to remove the data part. For example: if(response.features). Just one of those things that after the fact seemed pretty obvious. I could probably just changed it to parseJson(resp) and got the same result.

1 Like

Thanks, your example above helped me convert some code for Garadget

Feel free to look at my NOAA app for reference how to manipulate more of the api.

1 Like

Glad I could help. I'm not much of a programmer and I get a lot of my ideas from others.

I know the feeling...hahahha. But I'm quite happy once I figure it out. I converted the app and driver that was making a call every minute to check on the status of the device to async. I left the config calls straight httpget since they rarely (if ever now) will get called. I'm down to only a few 3rd party apps on my hub (tracing down slowdowns). Even though things have already gotten extremely better and I don't think this app was causing much of an issue...I figured it didn't hurt to convert. Plus I rely on the garage doors pretty heavily (they were the reason that I got into HA to begin with) so I couldn't just disable them.