1

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 property 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);
siavash90
  • 81
  • 1
  • 5

5 Answers5

1

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");
Amir Raminfar
  • 33,777
  • 7
  • 93
  • 123
0

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?

I82Much
  • 26,901
  • 13
  • 88
  • 119
0

getResourceAsStream is case sensitive. Make sure that your file-name matches exactly.

sgibly
  • 3,828
  • 1
  • 21
  • 21
0

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!

krock
  • 28,904
  • 13
  • 79
  • 85
Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
0

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.

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904