我有一个批处理作业,处理从源到目标的数据。偶尔,在作业触发后,我会立即看到NPE,它运行良好直到完成。看起来像第96行引发了错误,但是我不确定为什么在作业触发时发生?我认为这是错误的来源。
Node node = nodeList.item( 0 ).getAttributes().getNamedItem(DCDirectProcessorConstants.Tag_Response_Status);
Java代码
InputSource source = new InputSource();
source.setCharacterStream(new StringReader(response));
Document document = builder.parse(source);
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nodeList = document.getElementsByTagName(DCDirectProcessorConstants.Tag_Response_getValue);
Node node = nodeList.item( 0 ).getAttributes().getNamedItem(DCDirectProcessorConstants.Tag_Response_Status);
RatingSessionTO responseTO = XMLProcessingUtil.processResponseData(
ratingSessionTO, response, migrationConfig, data.toString());
/*DataMigrationDao dao = null;
if( migrationConfig.isOneTimeMigration() ){
dao = (DataMigrationDao) appContext.getBean("oneTimeMigrationDao");
}else{
dao = (DataMigrationDao) appContext.getBean("dailyMigrationDao");
}
*/
//This flow is explicitly for Catchup
if("1".equals(catchUp)){
if(kafkaFailure){
postMessageToKafka(responseTO,catchUp,dao);
}
else if(raterFailure){
//Handle rater failure
int ID = dao.getExternalID(responseTO);
if(ID != 0){
responseTO.setExternalID(ID);
}
migrateMessageToRaterInfo(responseTO,dao,catchUp);
}
else{ //Handle XML_Sessions failure
try{
if(dao.verifySessionID(responseTO) == 0){
// Defect 76093
if (node == null || DCDirectProcessorConstants.Status_Failure.equalsIgnoreCase(node.getTextContent())) {
logger.error("Retry failure.Source id already present in catchup "+ ratingSessionTO.getSrcId());
} else {
dao.migrateData(responseTO);
postMessageToKafka(responseTO, catchUp, dao);
migrateMessageToRaterInfo(responseTO, dao, catchUp);
}
} else{
logger.error("Source id already present in archive "+ratingSessionTO.getSrcId()+" Updating status to 'C'");
dao.updateCatchupCompletion(ratingSessionTO.getSrcId());
}
}
catch(Exception e){
logger.error("Catchup failed for "+ratingSessionTO.getSrcId()+" Status remains 'P'");
}
}
}
堆栈跟踪
java.lang.NullPointerException
com.mercuryinsurance.r3.util.MigrationExecutorThread.run(MigrationExecutorThread.java:96)[migration.jar :?] 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)[?:1.8.0_66] 在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)[?:1.8.0_66] 在java.lang.Thread.run(Thread.java:745)[?:1.8.0_66]
答案 0 :(得分:0)
从的Java文档NodeList.item()
:
“返回集合中第index项。如果索引大于或等于在所述列表中的节点的数量,这将返回空值。”
https://docs.oracle.com/javase/8/docs/api/org/w3c/dom/NodeList.html
为防止NullPointerException
,应在访问元素之前检查nodeLists的长度:
NodeList nodeList = document.getElementsByTagName(DCDirectProcessorConstants.Tag_Response_getValue);
if(nodeList.getLength() > 0) {
Node node = nodeList.item( 0 ).getAttributes().getNamedItem(DCDirectProcessorConstants.Tag_Response_Status);
...
} else {
// handle the case no node found
}
您的代码无法显示为什么尽管NullpointerException
仍能正常运行,但是我想您发布的代码段中有一些try / catch
用于处理和记录异常。