I’ve a weird problem of unable to instantiate a bean which is injected to another bean.
The PropertiesUtil is the bean in question. It’s injected to the LoginController class as follows in my sn-servlet.xml
<bean name="/Login.html" class="org.sn.auth.LoginController">
<property name="dbUtil" ref="dbUtil"/>
<property name="propertiesUtil" ref="propertiesUtil"/>
</bean>
and my PropertiesUtil.java is
public class PropertiesUtil {
private Properties properties;
public PropertiesUtil() {
properties = new Properties();
try {
properties.load(ClassLoader.getSystemResourceAsStream(
"/resources/messages.properties"));
}
catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
And the NullPointerException occurs at the line where I try to use the properties to load a resource. I’m really confused why it’s null when I’m clearly instanting it in the previous line.
I’ve also tried injecting the properties instance as a constructor-arg and also as a property from the sn-servlet.xml, but all in vain.
Is there something like I’m not supposed to do any operations in a constructor when that bean is spring-injected to some other class?
Thanks for any ideas!
Check out the javadoc for
ClassLoader.getResourceAsStream(). It returnsnullif the resource cannot be found. So it would seem that messages.properties cannot be found and the Properties.load() method will throw aNullPointerExceptionwhen trying to read from a nullInputStream.