0

I have a csv file that my class with the code below is trying to read that data to use in my project, but when i run the project, I have a NullPointerException like the csv file is null, but he isn't. The path of the csv file is right and apparently I don't have any error in my class too, what I am missing?

package main.java.br.com.quantumfinance.selecaoEstagio.leitor;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class LeitorDeArquivo {

    public List<String> lerArquivo() {

        try (InputStream resourceAsStream = LeitorDeArquivo.class.getResourceAsStream("/acoes.csv")) {
            // Leitura do arquivo.
            BufferedReader br = new BufferedReader(new InputStreamReader(resourceAsStream));
            // ignora a primeira linha
            br.readLine();
            List<String> cotacoes = new ArrayList<>();

            String linha;
            while ((linha = br.readLine()) != null) {
                cotacoes.add(linha);
            }
            return cotacoes;

        } catch (FileNotFoundException e) {
            System.out.println("Arquivo n�o encontrado.");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("Erro de IO.");
            e.printStackTrace();
        }
        throw new RuntimeException("Erro na leitura do arquivo, consulte o console para maiores detalhes.");
    }
}

This is the error that I receive when trying to run the project

Exception in thread "main" java.lang.NullPointerException
    at java.base/java.io.Reader.<init>(Reader.java:168)
    at java.base/java.io.InputStreamReader.<init>(InputStreamReader.java:76)
    at main.java.br.com.quantumfinance.selecaoEstagio.leitor.LeitorDeArquivo.lerArquivo(LeitorDeArquivo.java:17)
    at main.java.Questoes.main(Questoes.java:26)

This is the project structure

enter image description here

  • If you want to use `getResourceAsStream` then the file you want to read should be in `resources/` (right next to `java/` where your source code lives). I mean technically it needs to be on the classpath, but what I said is how to *get it on* the classpath in your usual maven builds. – Joachim Sauer Feb 02 '23 at 14:18
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – knittl Feb 02 '23 at 14:28
  • Wait: I just realized that your packages are called `main.java.br....` which is ... bad, your `br` directory should be directly in `src` the way your project is set up (with a corresponding change to your `package` statements). And the `acoes.csv` should then be directly inside the `src/` directory (i.e. next to where the `Questoes.java` should go) as well. – Joachim Sauer Feb 02 '23 at 14:35
  • I moved the file and the directory, but the project still don't running – Marco Aurélio Lopes Júnior Feb 02 '23 at 15:06
  • Please [edit] the question to show the current state of your code. Did you move `acoes.csv` into your `src` folder? – Joachim Sauer Feb 02 '23 at 16:52

0 Answers0