Spring ClassPathXmlApplicationContext NullPointerException

我是研究僧i 提交于 2020-01-03 18:57:36

问题


Trying to use spring. The xml file is under src. I've searched and cannot find the problem. Seems it cant find the xml file.

I get the following error:

    Exception in thread "main" java.lang.ExceptionInInitializerError
at org.springframework.core.env.AbstractEnvironment.suppressGetenvAccess(AbstractEnvironment.java:406)
at org.springframework.core.env.AbstractEnvironment.getSystemEnvironment(AbstractEnvironment.java:368)
at org.springframework.core.env.StandardEnvironment.customizePropertySources(StandardEnvironment.java:79)
at org.springframework.core.env.AbstractEnvironment.<init>(AbstractEnvironment.java:124)
at org.springframework.core.env.StandardEnvironment.<init>(StandardEnvironment.java:54)
at org.springframework.context.support.AbstractApplicationContext.createEnvironment(AbstractApplicationContext.java:444)
at org.springframework.context.support.AbstractApplicationContext.getEnvironment(AbstractApplicationContext.java:283)
at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.resolvePath(AbstractRefreshableConfigApplicationContext.java:122)
at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.setConfigLocations(AbstractRefreshableConfigApplicationContext.java:80)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:137)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at org.hancock.javabrains.DrawingApp.main(DrawingApp.java:13)
Caused by: java.lang.NullPointerException
at org.springframework.core.SpringProperties.<clinit>(SpringProperties.java:58)
... 12 more

This is the line causing an error (By changing Beans.xml to an erroneous name I still get same error):

ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");

The xml (Beans.xml):

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">

<bean id="helloWorld" class="org.hancock.javabrains.HelloWorld">
   <property name="message" value="Hello World!"/>
</bean>

</beans>

回答1:


Try this by giving the full file path

ApplicationContext ctx = new FileSystemXmlApplicationContext("Beans.xml");


来源:https://stackoverflow.com/questions/22864931/spring-classpathxmlapplicationcontext-nullpointerexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!