gpt4 book ai didi

java.lang.NullPointerException Spring Mvc

转载 作者:行者123 更新时间:2023-11-29 05:54:55 25 4
gpt4 key购买 nike

我得到一个 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)

最佳答案

问题是您通过新调用实例化了 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");
}
}

然后,在你的 Controller 中:

    BeanFactory.getOfficerRegistrationValidation().validate(....)

注意:新建 ClassPathXmlApplicationContext 时要小心,如果操作不当可能会导致内存泄漏。创建一个将新建它一次的静态工厂,并在任何后续调用中返回相同的实例。

关于java.lang.NullPointerException Spring Mvc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12573183/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com