我试图读取java中的xml,但在我的系统中出现了一个错误
您好,我正在编写代码,以便在我的机器上读取XML,它已经在另一台机器上运行良好,但在我的机器上,它在一行代码中出现错误
它读取500600个xml,最后创建一个电子表格 但在错误的第41行,我认为它在函数a中
遵守守则
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package teste;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
*
* @author francielle.garcia
*/
public class Sistema {
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {
DocumentBuilderFactory fabrica = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = fabrica.newDocumentBuilder();
Document xml;
Element e;
File a[];
File lerxml = new File("C:\\Users\\francielle.garcia\\Desktop\\Ler XML\\localXML.xml");
xml = builder.parse(lerxml);
NodeList locais = xml.getElementsByTagName("localXML");
e = (Element) locais.item(0);
String local = e.getTextContent();
File diretorio = new File(local);
a = diretorio.listFiles();
int i;
for(i = 0; i < a.length; i++){ // linha do erro
xml = builder.parse(a[i]);
NodeList payloads = xml.getElementsByTagName("payload");
e = (Element) payloads.item(0);
String payload = e.getTextContent();
NodeList timeStamps = xml.getElementsByTagName("stuMessages");
e = (Element) timeStamps.item(0);
String timeStamp = e.getAttribute("timeStamp");
File arquivo = new File("C:\\Users\\francielle.garcia\\Desktop\\Ler XML\\xml.csv");
arquivo.createNewFile(); //Caso voce queira criar um novo arquivo a partir de cada formulario use esse comando
try (FileWriter fw = new FileWriter(arquivo, true)) {
fw.write(payload + ";" + timeStamp+"\r\n");
}
}
System.out.println("Numero de Linhas Geradas: "+i);
}
跟踪错误
run:
Exception in thread "main" java.lang.NullPointerException
at teste.Sistema.main(Sistema.java:41)
C:\Users\francielle.garcia\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
FALHA NA CONSTRUÇÃO (tempo total: 0 segundos)
# 1 楼答案
这里
“a.length”正在生成NullPointerException,这是因为此处创建的文件
具有无效路径