你好,我正在编写代码以在我的计算机上读取xmls,它已经可以在另一台计算机上完美运行,但是在我的计算机上,它在一行代码中给出了错误
读取500、600 xml,最后创建一个电子表格 但是在错误的第41行,我认为它在函数a.length
中遵循代码
/*
* 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)
答案 0 :(得分:1)
此处
for(i = 0; i < a.length; i++){ // linha do erro
“ a.length”正在产生NullPointerException,这就是因为在此处创建的文件
File diretorio = new File(local);
路径无效