after define a property file in idea 10.5 when i try to use it,compiler show me Null Pointer Exception!I try anything that i think fix it among change pr开发者_运维知识库operty file path,... here is the code that i write: thanks. beginner programmer!!!
public class MainDlg{
Properties properties=new Properties();
public MainDlg() {
try {
InputStream reader=MainDlg.class.getResourceAsStream("propFile");
properties.load(reader);
Sounds like the reader
is null. Make sure the path is correct.
If property file is the root then you should
InputStream reader=MainDlg.class.getResourceAsStream("/propFile");
getResourceAsStream returns null if the resource cannot be found. Are you positive that, relative to the location of the MainDlg.class file, there is a folder called presentation, in which is a file called propFile?
getResourceAsStream is case sensitive
. Make sure that your file-name matches exactly.
Where did you put the propFile
? It should reside in the Source
root, however IDEA will not copy it by default to the classpath as there is no pattern for the empty extension in Settings
| Compiler
| Resource Patterns
.
It's recommended that you rename the file to something.properties
so that IDEA copies it to the output (classpath) and it will become available as a classpath resource.
If you don't want to rename the file, put it in some directory outside the source root and configure this directory as a library dependency in IDEA Project Structure
| Modules
| Dependencies
, this way it will be available from classpath.
probably you resources (prop files) are not put into the classes directory. In any case here is a tip ;) just before reading the property, print the following:
new java.util.File(".").getAbsolutePath()
This should give you a good idea of what is your actual current directory Hope this will resolve your issues. Good luck!
精彩评论