Hello I am writing the code to read in xmls on my machine, it runs perfect already on another machine but on my machine it gives an error in one line of code
it reads 500, 600 xml and in the end creates a spreadsheet but at line 41 of an error I think it is in the function a.length
follow the code
/*
* 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);
}
follow the error
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)