-1

I have a batch job which processes data from source to destination. Once in a while I see NPE immediately after the job triggers and it runs fine until it finishes. Looks like line 96 is throwing the error but I am not sure why when the job triggers? This is the line which I think is the source f the error.

Node node = nodeList.item( 0 ).getAttributes().getNamedItem(DCDirectProcessorConstants.Tag_Response_Status);

Java Code

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'");
                    }
                }
            }

Stack Trace

java.lang.NullPointerException

at com.mercuryinsurance.r3.util.MigrationExecutorThread.run(MigrationExecutorThread.java:96) [migration.jar:?] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [?:1.8.0_66] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [?:1.8.0_66] at java.lang.Thread.run(Thread.java:745) [?:1.8.0_66]

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Mike
  • 53
  • 1
  • 8

1 Answers1

0

From the Java Doc of NodeList.item():

"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

To prevent the NullPointerException, you should check the nodeLists length before accessing the element:

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
}

Your code does not show why the job still runs fine despite the NullpointerException but I would guess there is some try / catch around the snippet you have posted that handles and logs the exception.

Dan_Maff
  • 311
  • 2
  • 7