0

Probably it is discussed somewhere but I failed to find it.

I need to load class properties (java.util.Properties) inside class static initialization block. This is to make possible to access some class general options even without its objects creation. To do so I need appropriate Class object. But of course access to such Class object fails on null object. Something like this.

Class Name {

    private static Properties properties;

    static {
        Name.properties = new Properties();
        Name.properties.load(Name.class.getResourceAsStream("Name.properties"));
    }

}

Any idea how to handle this situation?

UPDATE:
It was resource name (should be "/Name.properties" for my case). Everything else was OK. +1 for all meaningful answers from me and ... don't forget to check operations one by one :-).

7
  • Declare properties static too. Commented Nov 6, 2013 at 11:35
  • Thank you, it was actually done in original code, typo. Corrected. Commented Nov 6, 2013 at 11:37
  • I hope following will help you: stackoverflow.com/questions/1044855/… Commented Nov 6, 2013 at 11:37
  • 1
    If you're setting Name.properties to a new Properties object, the only thing that could be null is the value returned by getResourceAsStream. Make sure Name.properties is in the same directory as Name.class in your .jar file. Commented Nov 6, 2013 at 11:45
  • Just found the same thing. Commented Nov 6, 2013 at 11:49

3 Answers 3

3

properties field must be static. And before load you need to initialize static variable with proeprties = new Properties() after that you can invoke load

Sign up to request clarification or add additional context in comments.

Comments

1

Declare properties as static and initialize

static Properties properties;

or

static Properties properties = new Properties();

and static block should be

static {
    try {
        properties = new Properties(); //if you have not initialize it already
        Name.properties.load(Name.class.getResourceAsStream("Name.properties"));
    } catch (IOException e) {
        throw new ExceptionInInitializerError(e); //or some message in constructor
    }
}

You need to catch IOException while loading properties file

4 Comments

printStackTrace is one of the worst ways to handle the exception, unless Roman actually doesn't care if the properties file was loaded. The correct course of action is throw new RuntimeException(e);.
I know its just that I wanted to be specific to question, the OP needs to handle exceptions properly. I won't be writing entire code for him :)
An ExceptionInInitializerError would be even better IMHO: "Signals that an unexpected exception has occurred in a static initializer. An ExceptionInInitializerError is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable."
@GyroGearless yes ExceptionInInitializerError would be more appropriate
0

Final code based on all suggestions is like this:

Class Name {

    private static final Properties properties = new Properties();

    static {
        try {
            InputStream stream = Name.class.getResourceAsStream("/Name.properties");
            if (stream == null) {
                throw new ExceptionInInitializerError("Failed to open properties stream.");
            }
            Name.properties.load(stream);
        } catch (IOException e) {
            throw new ExceptionInInitializerError("Failed to load properties.");
        }
    }
}

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.