java - 从文件读取时出现 NullPointerException

标签 java exception pointers null

我在运行 Java 程序时遇到 java.lang.NullPointerException 错误,在阅读完该错误后,我想我明白了该错误的含义,但我仍然不确定如何修复它。

import java.util.ArrayList;
import java.io.FileInputStream;
import java.io.DataInputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintStream;
public class readFile {


public static ArrayList<String> pullFirst(String fileName) throws IOException{
    String filen = "C:\\Users\\Steven\\Desktop\\Tests\\wunderground\\outputTweetsWeather.txt";
    ArrayList<String> arl = new ArrayList<String>(); // java 6 ArrayList 

    FileInputStream fs = new FileInputStream(filen); // this is how you access a file in java
    DataInputStream din = new DataInputStream(fs);
    BufferedReader bin = new BufferedReader(new InputStreamReader(din));

    String line;    
    while(!(line = bin.readLine()).startsWith("String index out of range")){ // read each line in the file while they exist
        arl.add(line);                // add them to the array list
    }

    return arl;                                  // return the array list
}

public static void main(String[] args, int j) {

    // this is just demo code to prove it works so you can check the output.
    try{
    ArrayList<String> rVal = pullFirst("testFile.txt");
        for(String a : rVal){
            //System.out.println(a + "\n"); // insert write to file code here
           if(a.startsWith("+t")){
       //          System.out.println(a);
           }
        }
        int i = 36;


        PrintStream out5 = new PrintStream(new FileOutputStream("forpopUp1.txt", true));

        System.setOut(out5);
     System.out.println(rVal.get(2 + j*i)); // Display Value


    }catch(Exception e){
        System.out.print("Problem in readFile" + e);
    }

}
}

现在我不确定这是否与我的主线有关

public static void main(String[] args, int j) {

但是,据我所知,我必须这样做,因为这个类是在另一个类的 if 条件运行后执行的,使用行

readFile.main(args, counterForreadFile);

这是 StackTrace 的结果:

java.lang.NullPointerException
at readFile.pullFirst(readFile.java:22)
at readFile.main(readFile.java:33)

如有任何帮助,我们将不胜感激!

最佳答案

我认为错误是在您读取的文件已用完时出现的:

读取文件时:

while(!(line = bin.readLine()).startsWith("String index out of range")){ 
  ...
}

当文件耗尽时,BufferedReader 将返回 null

当您执行 (line=bin.readLine()).startsWith(..) 时,您实际上尝试在 null 上调用 startsWith() code> 在这种情况下 - 因此会出现错误。

要解决这个问题,您应该迭代为:

while ((line = bin.readLine()) != null) { 
  if (line.startsWith("String index out of range")) break; 
  //rest of the code
}

当读者精疲力竭时,这可以为您提供null安全性。

虽然这绝对是一个问题 - 在您提供完整的堆栈跟踪之前,我无法确定这就是您遇到的确切问题。

<小时/>

附注正如我在评论中已经说过的 - 获取堆栈跟踪(它提供了有关该问题的更多信息),您可以将其添加到您的 catch block 中,因此它将是这样的:

}catch(Exception e){
    System.out.print("Problem in readFile" + e);
    e.printStackTrace();
}

另一种方法是将 main() 声明为 throws Exception,并删除 catch block (以及 try > 也阻止)。

关于java - 从文件读取时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12965270/

相关文章:

swift - “ fatal error :在展开可选值时意外发现nil”是什么意思?

c - 使用双指针切换变量的值

java - 我如何在这些 GUI 框架之间转换?

java - 将单选按钮选择转换为数据

java - 在java中调用shell脚本?

python - Python : Handling requests exceptions the right way

java - 导航到上一个 Activity

database - 哪个更好 : Catching database exception or creating manual check?

c++ - int *p 和 int* p 有什么区别

c++ - 将指针留在函数中会导致内存泄漏吗?