【发布时间】:2017-07-26 02:21:50
【问题描述】:
List<String> assignedGroups = new ArrayList<>();
assignedGroups.add("Group1");
assignedGroups.add("Group2");
assignedGroups.add("Group3");
assignedGroups.add("Group4");
assignedGroups.forEach(assignedGroup -> {
System.out.println("");
System.out.println("Retrieving tickets from " + assignedGroup + " ...");
System.out.println("");
/**
* @param args
* Cloud Server Deployment - Create server in CIA in Active state
*/
// Create SOAP Response
String qualification = "'Priority' == \"Critical\" AND 'Assigned Group' = \""+ assignedGroup + "\"";
SOAPMessage soapResponse = null;
try {
soapResponse = soapConnection.call(createSOAPRequest(qualification), url);
} catch (Exception e) {
e.printStackTrace();
}
try {
if (soapResponse != null) {
soapResponse.writeTo(System.out);
System.out.println("Giant Spaghetti Monster!");
}
} catch (SOAPException | IOException e) {
e.printStackTrace();
}
// Print the SOAP Response
try {
printSOAPResponse(soapResponse, soapConnection);
} catch (Exception e) {
e.printStackTrace();
}
});
有时我在尝试检索 SOAP 响应时收到 NullPointerException:
<?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><soapenv:Fault><faultcode>soapenv:Server.userException</faultcode><faultstring>ERROR (302): Entry does not exist in database; </faultstring><detail><ns1:hostname xmlns:ns1="http://xml.apache.org/axis/">WEBDKBA866</ns1:hostname></detail></soapenv:Fault></soapenv:Body></soapenv:Envelope>
java.lang.NullPointerException
at src.RemedySOAPWebService.printSOAPResponse(RemedySOAPWebService.java:218)
at src.RemedySOAPWebService.lambda$main$0(RemedySOAPWebService.java:78)
at java.util.ArrayList.forEach(ArrayList.java:1249)
at src.RemedySOAPWebService.main(RemedySOAPWebService.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
现在,除了修复异常,有没有办法忽略它并继续迭代列表中的下一个组?
例如,NPE 出现在Group3。我可以转移到Group4,而不是让我的脚本崩溃吗?我已经打印了出现 NPE 的Giant Spaghetti Monster。这可能吗?
【问题讨论】:
-
只需将产生异常的代码行包装在 try catch 块中即可,但请确保处理可能有空指针或空数组的情况...
标签: java list arraylist iteration try-catch