我在批处理作业中遇到空指针

I am facing Null Pointer during batch job

我有一个处理从源到目标的数据的批处理作业。偶尔我会在作业触发后立即看到 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]

来自 NodeList.item() 的 Java 文档:

"Returns the indexth item in the collection. If index is greater than or equal to the number of nodes in the list, this returns null."

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