我正在尝试搜索集合中的项目,但我最终会出现NullPointerException,即使我知道这些项目存在。这是我搜索集合的代码:
Collection<Item> newnetSWLicense = CollectionUtils.select(sortedItems, new Predicate() {
public boolean evaluate(Object o) {
Item item = (Item)o;
return item.getVendor().equals("NewNet") && item.getItemType().equals("SW") && item.getDescription().contains("Connect7") ||
item.getVendor().equals("NewNet") && item.getItemType().equals("SW") && item.getDescription().contains("C7");
}
});
当项目描述如下时,一切正常:
Connect7 Simplex SGC License
或
Connect7 Redundant SGC License
但是当描述包含“C7 3.0 core TCAP 8K dialogs simplex
”时,它会返回NullPointerException:
10:19:41,664 ERROR [STDERR] java.lang.NullPointerException
10:19:41,664 ERROR [STDERR] at org.apache.jsp.View.bom.ListBom_jsp$3.evaluate(ListBom_jsp.java:286)
10:19:41,664 ERROR [STDERR] at org.apache.commons.collections.CollectionUtils.select(CollectionUtils.java:517)
10:19:41,664 ERROR [STDERR] at org.apache.commons.collections.CollectionUtils.select(CollectionUtils.java:498)
10:19:41,664 ERROR [STDERR] at org.apache.jsp.View.bom.ListBom_jsp._jspService(ListBom_jsp.java:283)
10:19:41,664 ERROR [STDERR] at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
10:19:41,664 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
10:19:41,664 ERROR [STDERR] at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
10:19:41,664 ERROR [STDERR] at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:322)
10:19:41,664 ERROR [STDERR] at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:249)
10:19:41,664 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
10:19:41,664 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
10:19:41,664 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
10:19:41,664 ERROR [STDERR] at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
10:19:41,664 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
10:19:41,664 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
10:19:41,664 ERROR [STDERR] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
10:19:41,664 ERROR [STDERR] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
10:19:41,665 ERROR [STDERR] at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
10:19:41,665 ERROR [STDERR] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:525)
10:19:41,665 ERROR [STDERR] at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
10:19:41,665 ERROR [STDERR] at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
10:19:41,665 ERROR [STDERR] at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
10:19:41,665 ERROR [STDERR] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
10:19:41,665 ERROR [STDERR] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
10:19:41,665 ERROR [STDERR] at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
10:19:41,665 ERROR [STDERR] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
10:19:41,665 ERROR [STDERR] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
10:19:41,665 ERROR [STDERR] at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
10:19:41,665 ERROR [STDERR] at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
10:19:41,665 ERROR [STDERR] at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
10:19:41,665 ERROR [STDERR] at java.lang.Thread.run(Thread.java:722)
知道如何避免这种异常吗?顺便说一句。供应商和项目类型100%正确。
答案 0 :(得分:0)
问题必须与您的代码有关:
return item.getVendor().equals("NewNet") && item.getItemType().equals("SW") &&
item.getDescription().contains("Connect7") ||
item.getVendor().equals("NewNet") && item.getItemType().equals("SW") &&
item.getDescription().contains("C7");
不要将所有内容都放在一个return语句中;首先为供应商和描述提取本地变量。检查变量null
。
if (item == null) return false;
final String v = item.getVendor(), d = item.getDescription(),
t = item.getItemType();
return d != null && "NewNet".equals(v) && "SW".equals(t) &&
(d.contains("C7") || d.contains("Connect7"));