Error initializing a String Array

0

I am creating a program that lists the contents of a file, the problem that when I initialize my String array I have an error that is not recognized. This is the code:

        File origen = new File("TEXTO.TXT");


    InputStream in = new FileInputStream(origen);
    FileOutputStream destino = new FileOutputStream(new File("COPIADO.TXT"));
    ObjectOutputStream oOS = new ObjectOutputStream(destino);

    byte[] buf = new byte[1024];
    int len;


    while ((len = in.read(buf)) > 0) {

        oOS.write(buf, 0, len);

    }



    in.close();
    oOS.close();

    String[] destino1 = origen.list();
    for (int i = 0; i<destino1.length; i++) {
        System.out.println(destino1[i]);

    }

Error

Exception in thread "main" java.lang.NullPointerException
at ficheros.Ficheros.main(Ficheros.java:37)

    for (int i = 0; i<destino1.length; i++) {
        System.out.println(destino1[i]);

    }
    
asked by zzxbx 11.01.2018 в 19:51
source

1 answer

2

The error is that it already has an object with the name destination and that same name is being added to the array:

 File origen = new File("TEXTO.TXT");


InputStream in = new FileInputStream(origen);
 /*Primera definición*/
FileOutputStream destino = new FileOutputStream(new File("COPIADO.TXT"));

ObjectOutputStream oOS = new ObjectOutputStream(destino);

byte[] buf = new byte[1024];
int len;
 /*Segunda definición*/
String[] destino = origen.list();

while ((len = in.read(buf)) > 0) {

    oOS.write(buf, 0, len);

}

in.close();
oOS.close();
    
answered by 11.01.2018 / 19:57
source