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);
1
  • 1
    You have asked 4 questions and none accepted answers. Please accept some questions before asking more. Commented Aug 24, 2011 at 19:33

5 Answers 5

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");
Sign up to request clarification or add additional context in comments.

7 Comments

I know reader is null but I don't know why!!When i get Ctrl and mouse over on filename, Idea high light it.
Can you share where .java and your prop file is? We can help then
ok!prop file is in src directory and MainDlg is in presentation package!I copy the file in this package but it's not fix yet!!!
I test it before but not work!I don't know where is the problem!! thanks
Is this a maven project? I think the problem is your IDE or Maven is not coping the files over.
|
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?

Comments

0

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

Comments

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.

Comments

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.