首页 > 解决方案 > 我在批处理作业期间面临空指针

问题描述

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

标签: javanullpointerexceptionbatch-processing

解决方案


来自Java Doc NodeList.item()

“返回集合中的第 index 个项目。如果 index 大于或等于列表中的节点数,则返回 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您发布的代码片段周围有一些处理和记录异常。


推荐阅读