Fileinputstream nullpointerexeption Java

时间:2019-06-11 17:49:38

标签: java nullpointerexception fileinputstream

我在StackOverflow上搜索了此错误,但是发现任何主题都可以解决我的问题。我有这段代码打开一个.txt文件并初始化对象变量,然后出现此错误。

public void lerDoArquivo() {
    try {
        FileReader ler = new FileReader("Menu_de_itens.txt");
        BufferedReader reader = new BufferedReader(ler);
        String linha;

        while ((linha = reader.readLine()) != null) {
            processaEntrada(linha);
        }

        reader.close();
    } 
    catch (IOException e) {
        e.printStackTrace();
    }  
}

这是我的stracktrace:

Exception in thread "main" java.lang.NullPointerException
    at java.io.FileInputStream.<init>(FileInputStream.java:130)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at java.io.FileReader.<init>(FileReader.java:58)
    at ItemMenuDAOTxt.lerDoArquivo(ItemMenuDAOTxt.java:22)
    at ItemMenuDAO.<init>(ItemMenuDAO.java:16)
    at ItemMenuDAOTxt.<init>(ItemMenuDAOTxt.java:14)
    at ControleDeMenu.<init>(ControleDeMenu.java:21)
    at FastFoodOO.main(FastFoodOO.java:11)

1 个答案:

答案 0 :(得分:0)

确认您可以读取文件,很可能找不到该文件。

from copy import deepcopy

gsLst = gradient_stops._gsLst
new_gs = deepcopy(gsLst[0])
gsLst.append(new_gs)
new_gradient_stop = gradient_stops[-1]