在JAVA空指针异常中同时编辑文件

时间:2018-09-20 17:22:41

标签: java xml concurrency executorservice

我正在处理带有8931条记录的XML字符串。我要访问一些标签,然后在保存之前执行一些拆分功能和修剪功能。我尝试使用简单的循环,但要花很长时间(将近10分钟)。我尝试使用java executor服务来并行执行功能。但是我不知道我要预先更改多少个标签。所以我不能使用内联运行任务。我正在尝试创建一个子类,该子类将处理更改文件的代码,但是当获取具有所需标签的节点列表时,我在子类run方法中获取了空指针异常。 doc是全局变量。因此,在访问它之前,它应该包含一些内容,因为我在调用提交之前就对其进行了分配。如果还有其他方法可以有效地做到这一点,请提出建议。

private void normalize_xml(Request_return req_xml) {
System.out.println("In normalize_xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
    ExecutorService executor = Executors.newFixedThreadPool(10);
    db = dbf.newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(req_xml.soapxml));
    try {
        doc = db.parse(is);
        NodeList nList = doc.getElementsByTagName("b:recordCount");
        for (int temp = 0; temp < nList.getLength(); temp++) {
            Node nNode = nList.item(temp);
            req_xml.set_count(Integer.parseInt(nNode.getTextContent()));
            System.out.println("count tag value:"+nList.getLength());
            System.out.println("Record count :"+Integer.parseInt(nNode.getTextContent()));                  
        }
        System.out.println("Starting id normalization");
    if(xf_local.hasid==true) {
            xf_local.dateFields+=","+"id";                          
        }
        attri = xf_local.dateFields.split(",");
        Runnable task[];
        for(l=0;l<attri.length;l++) { 
            RunnableImpl task_me = new Requests().new RunnableImpl();
            task_me.tagname=attri[l];
            executor.submit(task_me);
        }
        executor.shutdown();
    } catch (SAXException e) {
        System.out.println("Something went wrong with parser. "+e);
    } 
        System.out.println("Starting saving");
        TransformerFactory transformerFactory = TransformerFactory
                .newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File("Z:/files/sample_xml.xml"));
        try {
            transformer.transform(source, result);
        } catch (TransformerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("Done");
} catch (ParserConfigurationException e1) {
    System.out.println("Parser Configuration Exception. "+e1);      
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (TransformerConfigurationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
}

private class RunnableImpl implements Runnable { 
String tagname="";

public void run() 
{ 
     System.out.println("Executing Task inside for thread: " + tagname);
        try {
            System.out.println("1");
            NodeList nList_id = doc.getElementsByTagName("c:"+tagname);
            System.out.println("2");
            for (int temp = 0; temp < nList_id.getLength(); temp++) {
                System.out.println(""+tagname+" tag value:"+temp);
                Node nNode = nList_id.item(temp);
                if(tagname.equals("id")) {
                    nNode.setTextContent(nNode.getTextContent().trim());
                }
                else {
                    nNode.setTextContent(nNode.getTextContent().split("T")[0]);                                                             
                }
            }
        } catch (Exception ex) {
            System.out.println(ex);
        }
} 
} 

这是我得到的输出

Response Code : 200
In normalize_xml
count tag value:1
Record count :8391
Starting id normalization
Starting saving
Executing Task inside for thread: endDate
Executing Task inside for thread: checkDate
java.lang.NullPointerException
java.lang.NullPointerException
Done

它正在尝试更改两个日期标签,并在“ T”处将其分割,然后更新XML中的值。每个标签必须做8391次相同的操作。 谢谢

0 个答案:

没有答案