I have a tree map of the form: < String, String[] >
There are values inside of my String[] that are null. When writing my results to a file, using the below code, I'm getting:
java.lang.NullPointerException
My current code for writing to my file is below, I'm trying to replace the nulls with an empty string.
new File(outFolder).mkdir();
File dir = new File(outFolder);
//get the file we're writing to
File outFile = new File(dir, "javaoutput.txt");
//create a writer
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outFile), "utf-8"))) {
for (Map.Entry<String, String[]> entry : allResults.entrySet()) {
writer.write(entry.getKey() + " "+ Arrays.toString(entry.getValue()).replace(null, ""));
writer.newLine();
}
Any thoughts?
Your issue arises from this method call: replace(null,"");
Check the implementation of replace()
The first parameter it takes is a CharSequence
and the first thing it does with this char sequence is call toString()
on it.
This is going to throw a NullPointerException
every time.
Arrays.toString()
however will replace null
values with "null"
so change your call to replace to be: replace("null", "");