0

コレクション内のアイテムを検索しようとしていますが、アイテムが存在することを知っていても、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%正しいです。

4

1 に答える 1

1

問題はコードにあるはずです:

 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"));
于 2012-07-25T08:34:08.890 に答える