我在运行我的 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)
任何帮助,将不胜感激!