NullPointer in GetHttprequestData()

remote_address = (StructKeyExists(GetHttprequestData().headers,'X-Forwarded-For') ? ListFirst(GetHttprequestData().headers['X-Forwarded-For'], ":") : CGI.REMOTE_ADDR)

lucee.runtime.exp.NativeException: java.lang.NullPointerException
  at org.apache.tomcat.util.http.NamesEnumerator.findNext(MimeHeaders.java:433)
  at org.apache.tomcat.util.http.NamesEnumerator.nextElement(MimeHeaders.java:459)
  at org.apache.tomcat.util.http.NamesEnumerator.nextElement(MimeHeaders.java:417)
  at lucee.runtime.functions.other.GetHTTPRequestData.call(GetHTTPRequestData.java:52)
  at lucee.runtime.functions.other.GetHTTPRequestData.call(GetHTTPRequestData.java:42)
  at iqelasticlog_cfm$cf.threadCall(/iqElasticLog.cfm:52)
  at lucee.runtime.thread.ChildThreadImpl.execute(ChildThreadImpl.java:204)
  at lucee.runtime.thread.ChildThreadImpl.run(ChildThreadImpl.java:147)
Caused by: java.lang.NullPointerException... 8 more

Not even sure how to debug or fix this

This is what I use (basically, you need to check that GetHttprequestData().headers itself also exists):

var ipAddress = CGI.REMOTE_ADDR;
 if (!isNull(getHttpRequestData()) and !isNull(getHttpRequestData().headers) and structKeyExists(getHttpRequestData().headers, 'x-forwarded-for') ){
 ipAddress = getHttpRequestData().headers['x-forwarded-for'];
}

How about just

var ipAddress = (getHttpRequestData().headers['x-forwarded-for'] ?: CGI.REMOTE_ADDR);
2 Likes

You are running that snippet inside cfthread?