Java 枚举空指针异常?
您好,我正在使用HttpServletRequest并尝试获取标题集 代码如下:Java 枚举空指针异常?,java,nullpointerexception,http-headers,request,enumeration,Java,Nullpointerexception,Http Headers,Request,Enumeration,您好,我正在使用HttpServletRequest并尝试获取标题集 代码如下: public static Map<String, String> getHeaders(HttpServletRequest request) { Map<String, String> headers = new HashMap<String, String>(); Enumeration<String> headerNames = request.
public static Map<String, String> getHeaders(HttpServletRequest request) {
Map<String, String> headers = new HashMap<String, String>();
Enumeration<String> headerNames = request.getHeaderNames();
if (headerNames != null) {
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
String header = request.getHeader(headerName);
headers.put(headerName, header);
}
}
return headers;
}
如果你们能帮我解决这个问题就太好了 我怀疑这个问题是由一个错误的请求引起的。以下是
findNext()
正在做的事情(在Tomcat 6.0.18中):
如果标头损坏,则getName(j)
可能返回null
。如果发生这种情况,那么equalsIgnoreCase
方法的ByteChunk
路径将抛出一个NPE
如果你想科学地追踪这一点,你需要:
- 获取请求的实际原始字节,并对其进行取证以确定损坏的性质(如果有)
- 设置一个测试工具,允许您在此请求上运行应用程序,并连接一个调试器。。。。并在源代码处捕获异常
private void findNext(){
next=null;
对于(;pos for(int j=0;jPost stacktrace并准确指示stacktrace中引用了哪些行。Stack trace:由:org.apache.tomcat.util.buf.ByteChunk.equalsIgnoreCase(ByteChunk.java:608)处的java.lang.NullPointerException引起,org.apache.tomcat.util.buf.MessageBytes.equalsIgnoreCase(MessageBytes.java:325)位于org.apache.tomcat.util.http.NamesEnumerator.findNext(MimeHeaders.java:414)的org.apache.tomcat.util.http.NamesEnumerator.nextElement(MimeHeaders.java:438)的org.apache.tomcat.util.http.NamesEnumerator.nextElement(MimeHeaders.java:396)在GenerateRequestTheHeaderMap…Tomcat的哪个版本?准确地说。问题:这是您的真实代码吗?即给出异常的实际代码吗?或者您是否对其进行了编辑以“澄清”它?这些可能很有用:以及
Stack trace : Caused by: java.lang.NullPointerException at org.apache.tomcat.util.buf.ByteChunk.equalsIgnoreCase(ByteChunk.java:608) at
org.apache.tomcat.util.buf.MessageBytes.equalsIgnoreCase(MessageBytes.java:325)
at org.apache.tomcat.util.http.NamesEnumerator.findNext(MimeHeaders.java:414) at org.apache.tomcat.util.http.NamesEnumerator.nextElement(MimeHeaders.java:438)
at org.apache.tomcat.util.http.NamesEnumerator.nextElement(MimeHeaders.java:396) at generateRequestHeaderMap...
private void findNext() {
next=null;
for( ; pos< size; pos++ ) {
next=headers.getName( pos ).toString();
for( int j=0; j<pos ; j++ ) {
if( headers.getName( j ).equalsIgnoreCase( next )) {
// duplicate.
next=null;
break;
}
}
if( next!=null ) {
// it's not a duplicate
break;
}
}
// next time findNext is called it will try the
// next element
pos++;
}
next=headers.getName( pos ).toString();
if( headers.getName( j ).equalsIgnoreCase( next )) {
private void findNext() {
next=null;
for( ; pos< size; pos++ ) {
// (4 lines changed): check mb for null as suggested here: https://stackoverflow.com/questions/37493552/enumeration-null-pointer-exception/37493888#37493888
MessageBytes mb = headers.getName( pos );
if (mb != null) {
next=mb.toString();
}
for( int j=0; j<pos ; j++ ) {
// (2 lines changed): check mb and nex for null as suggested here: https://stackoverflow.com/questions/37493552/enumeration-null-pointer-exception/37493888#37493888
mb = headers.getName( j );
if(mb != null && next != null && mb.equalsIgnoreCase( next )) {
// duplicate.
next=null;
break;
}
}
// new (just 1 comment line): if mb == null we assume next == null, thus it will be a duplicate (i.e. not found, causing no break)
if( next!=null ) {
// it's not a duplicate
break;
}
}
// next time findNext is called it will try the
// next element
pos++;
}