我在文件 MovieReader 中写了 getMethod,如果我在这个文件中打印这个方法,一切都运行良好。
import java.io.BufferedReader; // scanner
import java.io.FileReader;
public class MovieReader {
private static String text;
public static void main(String args[]) throws Exception {
FileReader file = new FileReader("C:/Users/krystian/Desktop/filmDateBaseProject/movies.txt");
BufferedReader reader = new BufferedReader(file);
text = "";
String line = reader.readLine();
while(line != null) {
text+= line +"\n";
line=reader.readLine();
}
reader.close();
System.out.println(getText()); // This method works
}
public static String getText() {
return text;
}
}
但是当我试图从其他文件中调用这个方法时,它正在打印 null
public class Userr{
public static void main(String args[]){
MovieReader user = new MovieReader();
System.out.println(user.getText());
}
}
你能帮我吗?