Delete java.lang.NullPointerException problem

1

What I try to do in this program is to pass a file that contains a series of tables as if it were an excel and to fit the data in the code so that the data is printed in the same way in java.lang. NullPointerException that I get at the end of my impression, I do not really know what is due so I would like you to help me please. According to the program my error is on line 25.

    try {
        String[] data = new String[47];
        String lineaTemporal;
        File archivo = new File("Mat.txt");
        FileReader fr = new FileReader(archivo);
        BufferedReader br = new BufferedReader(fr);

        String linea;

        for (int i = 0; i < 47; i++) {
            data[i] = br.readLine();
            lineaTemporal = data[i];
            String[] parts = lineaTemporal.split(" ");
            for (String part1 : parts) {
                System.out.print(part1);
                System.out.print("\t");
            }
            System.out.println("");
        }

        //while ((linea = br.readLine()) != null) {
        //    System.out.println(linea);
        //}
    } catch (Exception e) {
        e.printStackTrace();
    }
    
asked by ASASCED 11.03.2018 в 21:41
source

1 answer

1

Try this code:

try {
        String[] data = new String[47];
        String lineaTemporal;
        File archivo = new File("Mat.txt");
        FileReader fr = new FileReader(archivo);
        BufferedReader br = new BufferedReader(fr);

        String linea;
        long i = 0;

        while ((sCadena = br.readLine())!=null) {
            data[i] = br.readLine();
            lineaTemporal = data[i];
            String[] parts = lineaTemporal.split(" ");
            for (String part1 : parts) {
                System.out.print(part1);
                System.out.print("\t");
            }
            System.out.println("");
            i++;
        }

        //while ((linea = br.readLine()) != null) {
        //    System.out.println(linea);
        //}
    } catch (Exception e) {
        e.printStackTrace();
    } catch (FileNotFoundException fnfe){
      fnfe.printStackTrace();
    } catch (IOException ioe){
      ioe.printStackTrace();
    }

This way you will only fill the amount of lines that the file has, you must be careful because if the file has more than 47 lines the arrangement is broken.

    
answered by 11.03.2018 / 21:59
source