Many tutorials for reading a file from a resources folder use class loader. However, using that method I cannot get past problems with static warnings and the result is always a null pointer exception.
public class Test {
public static void main(String[] args) {
StringBuilder contentBuilder=new StringBuilder();
ClassLoader classLoader=Test.class.getClassLoader();
File file=new File(classLoader.getSystemResource("test.html").getFile());
try {
BufferedReader buffer=new BufferedReader(new FileReader(file));
String sCurrentLine="";
while ((sCurrentLine=buffer.readLine())!=null) {
contentBuilder.append(sCurrentLine);
}
buffer.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
String content=contentBuilder.toString();
System.out.println("content="+content);
}
}
The warning from my IDE on the "File" line is:
The static method getSystemResource(String) from the type ClassLoader should be accessed in a static way
I cannot figure out how to eliminate the warning and if I just ignore it and run the code I get a null pointer exception. Why am I getting this and how do I fix it? TIA.