2

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:

enter image description here

Erran Morad
  • 4,563
  • 10
  • 43
  • 72
mrjobs
  • 31
  • 5
  • 2
    Please re-open my question. This is not another NPE/NullPointerException question. The NPE question that I have been pointed to, is based on a toy example. My question is different. It feels like a robot has flagged my question. – mrjobs Nov 21 '17 at 07:13
  • @suresh atta - Can you please reopen my question and also read a question before you close it ? – mrjobs Nov 21 '17 at 07:29
  • If you're not running a unit test, then your resources are in the wrong folder. Therefore, the file resource is null – OneCricketeer Nov 21 '17 at 07:39
  • @cricket_007 - That is the project structure and code which I have been given. Should I ask for it to be changed ? I am unable to understand my mistake. I'd appreciate it if you could suggest any google links or articles to lead me in the right direction. thanks. Also, could you please re-open the question ? – mrjobs Nov 21 '17 at 07:52
  • `src/test` is only applicable during the test Maven compilation stages. If you want your code to work as you've shown, you must use `src/main/resources` – OneCricketeer Nov 21 '17 at 07:57
  • @cricket_007 - Actually, I don't know why someone would want to use this class loader method to load text files and such. Is this approach advisable ? Btw, I also made an alternate function which works fine. It uses file reader, buffered reader & path relative to root of project. I wonder if this approach is more sensible. – mrjobs Nov 21 '17 at 19:03
  • "path relative to the root of the project"... And what happens when you run your application from an actual compiled JAR file? Do you want to copy the file along with the JAR? If it is read from the classpath, it is **inside** the JAR and able to be read from any machine that runs Java. That is the primary reason it is advisable. Otherwise, the file needs to be given a full path, not an absolute one – OneCricketeer Nov 21 '17 at 22:22
  • @cricket_007 - Thanks. This code will not be converted to a jar because its not needed. I wonder what this code is doing. I am reading up a bit on class loaders to figure it out. Yonran's answer in another question says that we must never use context class loaders. https://stackoverflow.com/questions/1771679/difference-between-threads-context-class-loader-and-normal-classloader. I wonder if that is applicable to the above code. – mrjobs Nov 22 '17 at 18:33
  • I've never found a need to use a Thread's classloader. I've always used the class's – OneCricketeer Nov 22 '17 at 20:44
  • A use of such code _might_ be within an OSGi framework where individual bundles (read JARs) have their own classloader and only have visibility of the packages within the their own bundle or those exported by other bundles. Here you are trying to load a file which could reside within another bundle in a package which is not visible to another bundle that needs to load it. One way around it is to load is using the Classloader for the bundle which _does_ contain the file. – D-Dᴙum Dec 04 '17 at 08:03

0 Answers0