java.lang.NullPointerException Spring Mvc
Posted
技术标签:
【中文标题】java.lang.NullPointerException Spring Mvc【英文标题】: 【发布时间】:2012-09-16 09:27:21 【问题描述】:我得到一个 java.lang.NullPointerException:
我为 validateUserNameManager 设置了 getter 和 setter。 我正在使用基于 xml 的配置,而不是使用 @Autowire。 当处理到达 validateUserNameManager 时,它会得到 NP 异常,我不确定我的配置和接线是否准确。
if (validateUserNameManager.DoesUserNameExist(officer.getUserName()) == true)
errors.rejectValue("userName", "userName.exist");
OfficerRegistrationValidation.java
public class OfficerRegistrationValidation implements Validator
private final Logger logger = Logger.getLogger(getClass());
private ValidateUserNameManager validateUserNameManager;
public boolean supports(Class<?> clazz)
return Officers.class.equals(clazz);
public void validate(Object target, Errors errors)
Officers officer = (Officers) target;
if (officer.getUserName() == null)
errors.rejectValue("userName", "userName.required");
else
String userName = officer.getUserName();
logger.info("OfficerRegistrationValidation - UserName is not null so going to check if its valid for :" + userName);
try
logger.info("OfficerRegistrationValidation - Just before try.....catch block...userName is :" + userName);
logger.info("OfficerRegistrationValidation - about to evaluate if (validateUserNameManager.DoesUserNameExist(officer.getUserName()) == true)" );
if (validateUserNameManager.DoesUserNameExist(officer.getUserName()) == true)
errors.rejectValue("userName", "userName.exist");
catch (Exception e)
logger.info("OfficerRegistrationValidation - Error Occured When validating UserName");
logger.error("Message", e);
errors.rejectValue("userName", "userName.error");
/**
* @return the validateUserNameManager
*/
public ValidateUserNameManager getValidateUserNameManager()
logger.info("Getting - ValidateUserNameManager");
return validateUserNameManager;
/**
* @param validateUserNameManager the validateUserNameManager to set
*/
public void setValidateUserNameManager(
ValidateUserNameManager validateUserNameManager)
logger.info("Setting - ValidateUserNameManager");
this.validateUserNameManager = validateUserNameManager;
ApplicationContext.xml
<bean id="officerRegistrationValidation" class="com.crimetrack.service.OfficerRegistrationValidation">
<property name="validateUserNameManager" ref="validateUserNameManager"/>
</bean>
<bean id="validateUserNameManager" class="com.crimetrack.service.ValidateUserNameManager">
<property name="officerDao" ref="officerDao"/>
</bean>
<bean id="officerDao" class="com.crimetrack.jdbc.JdbcOfficersDAO" >
<property name="dataSource" ref="dataSource" />
</bean>
Servlet.xml
<bean name="/officer_registration.htm" class="com.crimetrack.web.OfficerRegistrationController">
<property name="divisionManager" ref="divisionManager" />
<property name="positionManager" ref="positionManager" />
<property name="genderManager" ref="genderManager"/>
</bean>
<!-- __________________________________________________________________________________________________ -->
<bean name="/validateUserName.htm" class="com.crimetrack.web.OfficerRegistrationController">
<property name="validateUserNameManager" ref="validateUserNameManager"/>
</bean>
<!-- __________________________________________________________________________________________________ -->
堆栈跟踪:
47840 [http-8084-1] DEBUG org.springframework.beans.TypeConverterDelegate - Converting String to [class java.lang.String] using property editor [org.springframework.beans.propertyeditors.StringTrimmerEditor@7079b2]
47840 [http-8084-1] INFO com.crimetrack.service.OfficerRegistrationValidation - OfficerRegistrationValidation - UserName is not null so going to check if its valid for :adminzz
47840 [http-8084-1] INFO com.crimetrack.service.OfficerRegistrationValidation - OfficerRegistrationValidation - Just before try.....catch block...userName is :adminzz
47840 [http-8084-1] INFO com.crimetrack.service.OfficerRegistrationValidation - OfficerRegistrationValidation - about to evaluate if (validateUserNameManager.DoesUserNameExist(officer.getUserName()) == true)
47840 [http-8084-1] INFO com.crimetrack.service.OfficerRegistrationValidation - OfficerRegistrationValidation - Error Occured When validating UserName
47840 [http-8084-1] ERROR com.crimetrack.service.OfficerRegistrationValidation - Message
java.lang.NullPointerException
at com.crimetrack.service.OfficerRegistrationValidation.validate(OfficerRegistrationValidation.java:51)
at org.springframework.validation.DataBinder.validate(DataBinder.java:725)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doBind(HandlerMethodInvoker.java:815)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:367)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
【问题讨论】:
@PaulTomblin if (validateUserNameManager.DoesUserNameExist(officer.getUserName()) == true) 这个功能有效,因为我在网络服务调用中使用它 你如何检索OfficerRegistrationValidation-object? 你在哪里设置ValidateUserNameManager? 我没有做 setValidateUserNameManager 我认为当调用函数并且容器将通过 xml 文件设置它时会发生这种情况。如果我错了,请告诉我我必须在哪里执行 setValidateUserNameManager 【参考方案1】:问题是您通过新调用实例化了 OfficerRegistrationValidaton。这意味着 Spring 对对象一无所知,因此不会将任何东西连接到其中。
你需要通过Spring机制加载对象,要么通过@Autowire / @Inject,要么通过创建BeanFactory,像这样:
public final class BeanFactory()
private static ClassPathXmlApplicationContext context;
private static ClassPathXmlApplicationContext getContext()
if (context== null)
context = new ClassPathXmlApplicationContext("applicationContext.xml");
return context;
public static OfficerRegistrationValidation getOfficerRegistrationValidation()
return (OfficerRegistrationValidation) getContext().getBean("officerRegistrationValidation");
然后,在您的控制器中:
BeanFactory.getOfficerRegistrationValidation().validate(....)
注意:更新 ClassPathXmlApplicationContext 时要小心,如果操作不当,可能会导致内存泄漏。创建一个静态工厂,该工厂将新建一次,并在任何后续调用中返回相同的实例。
【讨论】:
这应该在 applicationcontext.xml 文件的什么地方完成?我还是不清楚,请您详细说明 不在您的应用程序上下文中,在 java 类中。该方法加载构造函数中指定的 applicationContext(s),并使 bean 可用。但是你真的应该使用自动装配,它让事情变得更容易.. 我从未使用过 Autowire,你能告诉我我必须将 Autowire 注释放在哪里吗?它是在类上还是它的任何实例上? 你把它放在实例变量上,所以在控制器@AutowireofficerRegistrationValidationofficerRegistrationValidation。此外,您需要向 applicationContext 添加一些内容以启用我认为的自动装配,但不记得具体是什么。我相信谷歌会告诉你.. BeanFactory中的config是什么,是对象吗?以上是关于java.lang.NullPointerException Spring Mvc的主要内容,如果未能解决你的问题,请参考以下文章
亲測,Eclipse报"An error has occurred,See error log for more details. java.lang.NullPointerExce