0

I'm trying to read a excel file in a singleton so it is loaded only once. The excel file and the singleton are in the same package but getResourceAsStream returns null.

What could I be missing ?

public static synchronized HSSFWorkbook getUniqueInstance(String fileStr) {

    if (sInstance == null) {
        try {
            InputStream file = WBSingleton.class.getResourceAsStream(fileStr);
            sInstance = new HSSFWorkbook(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return sInstance;

}
Pokechu22
  • 4,984
  • 9
  • 37
  • 62
JDavid
  • 3
  • 5
  • 1
    this is not singleton. HSSFWorkbook can have many instances outside your class. – Adi Sep 07 '14 at 18:02

1 Answers1

0

Try this

WBSingleton.class.getClassLoader().getResourceAsStream(fileStr);

You can also try

URL url = getClass().getResource(fileStr); 
InputStream name = url.openStream();
Tkachuk_Evgen
  • 1,334
  • 11
  • 17