I came across the code given below which reads text from a file & converts it to a string. I have never used this approach for reading files. Is it advisable to read a file like this ? Can someone please help me to understand how to debug this ?
package com.temp;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class Junk {
public static void main(String [] args) {
String filePath = "sample1.txt";
try {
fileToString(filePath);
} catch (IOException e) {
e.printStackTrace();
}
}
public static String fileToString(String filePath) throws IOException {
String actualFilepath = Thread.currentThread().
getContextClassLoader().
getResource(filePath).
getPath();
File file = new File(actualFilepath);
String fileContents = FileUtils.readFileToString(file);
return fileContents;
}
}
I get the following exception: It looks like the parent class loader for "Launcher$AppClassLoader" is null & might be causing the issue. How do I solve this problem ?
Exception in thread "main" java.lang.NullPointerException
at com.temp.Junk.fileToString(Junk.java:24)
at com.temp.Junk.main(Junk.java:13)
This is my maven project structure: