Including a text file inside a jar file and reading it
Don't try to find a file as a "file" in a Jar file. Use resources instead.
Get a reference to the class or class loader and then on the class or class loader call getResourceAsStream(/* resource address */);
.
See similar questions below (avoid creating new questions if possible):
- Reading a resource file from within jar
- How do I read a resource file from a Java jar file?
- Accessing a file inside a .jar file
- How to read a file from JAR archive?
// add a leading slash to indicate 'search from the root of the class-path'
URL urlToDictionary = this.getClass().getResource("/" + "Dictionary.txt");
InputStream stream = urlToDictionary.openStream();
See also this answer.
Seems like an exact duplicate of this question: How do I access a config file inside the jar?
Further to your problem with NullPointerException
, I suggest not to make sure it doesn't happen, but rather prepare for it and handle it properly. I'd go even further to ask you to always check your variables for null value, it's a good thing to do.