AnsweredAssumed Answered

Strange problem: Cannot invoke method getText() on null object (in groovy script)

Question asked by marcin.wawrzyn on Jul 4, 2018
Latest reply on Jul 9, 2018 by marcin.wawrzyn

Hi everyone ! 

 

Im getting error from my data process script: 

 

Error executing data process; Caused by: Cannot invoke method getText() on null object (in groovy script); Caused by: Cannot invoke method getText() on null object

 

The problem is that it comes up everytime when processing more then 26 documents. When i try to run the process once again with the document that failed last time - the process runs success.

Here is the script, much appreciate if someone could advise anything !

 

import java.util.Properties;
import java.io.InputStream;
//
import org.jdom.input.SAXBuilder;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.xpath.XPath;
import org.jdom.output.XMLOutputter;

import com.boomi.execution.ExecutionUtil;

//Retrieve a handle to the Logger
logger = ExecutionUtil.getBaseLogger();

// BEGIN GLOBAL LOGIC //
logger.fine("Global Logic - Begin");

def accLocRefList = [];
//XPath xShipToAccLocRef;
//XPath xBillToAccLocRef;
//XPath xAccLocRefUniqe;

//xShipToAccLocRef = XPath.newInstance("/invoice/invoiceItem/customer/account/accountLocationRef");
xAccLocRef = XPath.newInstance("//accountLocationRef");
//xBillToAccLocRef = XPath.newInstance("/invoice/customer/account/accountLocationRef");
xAccLocRefUnique = XPath.newInstance("//accontLocationRefUnique");

logger.fine("Global Logic - End");
// END GLOBAL LOGIC //

for( int i = 0; i < dataContext.getDataCount(); i++ ) {
    logger.info("DataContext[" + i + "] Started");

    InputStream is = dataContext.getStream(i);
    Properties props = dataContext.getProperties(i);

    logger.fine("DataContext[" + i + "] Build XML Document");
    // Build XML Document
    SAXBuilder builder = new SAXBuilder();
    Document doc = builder.build(is);
    if ( doc == null ) {
        logger.error("DataContext[" + i + "] DOC = NULL!!");
    }
    logger.fine("DataContext[" + i + "] Initializing variables");
    // myShiptoValue = null;
    // myBilltoValue = null;
    isUniq = 0;
    logger.fine("DataContext[" + i + "] Grab xAccLocRef");
    // grab values from the current data
    // Create an XPath statement to search for the SHIP_TO element:
    // Select a single node and grab the actual value
    //eleMyElement = xShipToAccLocRef.selectSingleNode(doc);
    eleMyElements = xAccLocRef.selectNodes(doc);
    logger.fine("DataContext[" + i + "] Grab xAccLocRef - done");
    if (eleMyElements != null) {
        myBilltoValue = eleMyElements[0].getText();
        logger.info("DataContext[" + i + "] Grab BillTo - Value: " + myBilltoValue);
        myShiptoValue = eleMyElements[1].getText();
        logger.info("DataContext[" + i + "] Grab ShipTo - Value: " + myShiptoValue);
    } else {
        logger.error("DataContext[" + i + "] Grab - is NULL");
    }

    if ( myShiptoValue != null ) {
        if (i == 0) { //first document
            logger.info("DataContext[" + i + "] First Document");
            if ( myBilltoValue != null ) {
                if ( myBilltoValue == myShiptoValue ) {
                    logger.info("DataContext[" + i + "] BillTo = ShipTo");
                    accLocRefList.add(myBilltoValue);
                } else {
                    logger.info("DataContext[" + i + "] Address Locations are unique");
                    accLocRefList.add(myBilltoValue);
                    accLocRefList.add(myShiptoValue);
                    isUniq = 1;
                }
                logger.fine("DataContext[" + i + "] accLocRefList Count: "+ accLocRefList.size());
            }
        } else {
            logger.info("DataContext[" + i + "] Searching list");
            isUniq = 1;
            for ( li = 0; li < accLocRefList.size(); li++) {
                logger.fine("DataContext[" + i + "] accLocRef["+li+"]: "+ accLocRefList[li]);
                if ( accLocRefList[li] == myShiptoValue ) {
                    isUniq = 0;
                    break;
                }
            }
            if (isUniq == 1 ){
                logger.info("DataContext[" + i + "] ShipTo Address Location is unique");
                accLocRefList.add(myShiptoValue);
                logger.fine("DataContext[" + i + "] accLocRefList Count: "+ accLocRefList.size());
            }
        }
        if (isUniq == 1 ) {
            logger.info("DataContext[" + i + "] "+ myShiptoValue + " mark to load");
            // Create an XPath statement to search for the SHIP_TO element:
            // Select a single node and grab the actual value
            eleMyElement = xAccLocRefUnique.selectSingleNode(doc);
            if (eleMyElement != null) {
                logger.fine("DataContext[" + i + "] AccLocRefUnique set to Y");
                eleMyElement.setText("Y");
            } else {
                logger.error("DataContext[" + i + "] AccLocRefUnique is NULL");
            }
        }
    }

    // When you're finished manipulating the XML data, you'll need to output the Document's data to an InputStream to pass to the next step in your Process work flow.

    XMLOutputter outputter = new XMLOutputter();
    is = new ByteArrayInputStream(outputter.outputString(doc).getBytes("UTF-8"));

    dataContext.storeStream(is, props);
}

Outcomes