BeanInstantiationException - Constructor threw exception; nested exception is java.lang.NullPointerException
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(Cla开发者_运维技巧ssLoader.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 returns null
if the resource cannot be found. So it would seem that messages.properties cannot be found and the Properties.load() method will throw a NullPointerException
when trying to read from a null InputStream
.
精彩评论