-1

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)
Ketan R
  • 4,039
  • 1
  • 13
  • 15
  • you might have provided wrong input which is throwing NullPointerExecption, learn how to debug java code. this will help you in coding. – Mayur Jan 03 '20 at 13:07
  • 1
    Some remarks on your code: The array-brackets should be written after the type, not after the variable name since they influence the type (`File a[]` -> `File[] a`). --- Variable names should be speaking (`a` -> `files`, `e` -> `element`),... --- It is industry standard to write code and comments in English. – Turing85 Jan 03 '20 at 13:07

1 Answers1

1

here

for(i = 0; i < a.length; i++){ // linha do erro

the "a.length" is making the NullPointerException and thats because the File created here

 File diretorio = new File(local);

has invalid path

gygabyte
  • 176
  • 2
  • 12
  • I set the path manually and null sumio but the other continues: C: \ Users \ francielle.garcia \ AppData \ Local \ NetBeans \ Cache \ 8.2 \ executor-snippets \ run.xml: 53: Java returned: 1 CONSTRUCTION FAILURE (total time: 0 seconds) – Samuel Silva Jan 03 '20 at 13:34