我有一个基于spring MVC框架的简单库项目。我将添加一个新作者到我的作者表,但我得到NullPointerException。 我在下面提供了与此功能相关的添加类。任何人都可以说出了什么问题?
@Controller
public class AuthorController {
@RequestMapping("/addAuthor")
protected ModelAndView addAuthor() throws Exception {
ModelAndView model = new ModelAndView("addAuthor");
return model;
}
@RequestMapping("/addAuthorExecution")
protected ModelAndView addAuthorExecution(
@RequestParam("authorName") String authorName) throws Exception {
ApplicationContext cnx = new ClassPathXmlApplicationContext("spring.xml");
AuthorService authorService = (AuthorService) cnx.getBean("authorService");
authorService.addAuthor(authorName);
ModelAndView model = new ModelAndView("adminFunctionsPage");
model.addObject("successMsg", "Your request has been processed Successfully.");
return model;
}
}
服务类:
public class AuthorService {
@Autowired
Author author;
@Autowired
AuthorDAO authorDAO;
public void addAuthor(String authorName) throws ClassNotFoundException,
SQLException {
AuthorDAO authorDAO = new AuthorDAO();
authorDAO.addAuthor(authorName);
}
}
DAO课程:
public class AuthorDAO extends JdbcDaoSupport {
public void addAuthor(String authorName) {
String sql = "insert into tbl_author (authorName) values (?)";
this.getJdbcTemplate().update(sql, new Object[] { authorName });
}
private static final class AuthorMapper implements RowMapper<Author> {
@Autowired
Author author;
@Override
public Author mapRow(ResultSet rs, int rowNum) throws SQLException {
author.setAuthorId(rs.getInt("authorId"));
author.setAuthorName(rs.getString("authorName"));
return author;
}
}
}
模特课程:
public class Author {
int authorId;
String authorName;
public int getAuthorId() {
return authorId;
}
public void setAuthorId(int authorId) {
this.authorId = authorId;
}
public String getAuthorName() {
return authorName;
}
public void setAuthorName(String authorName) {
this.authorName = authorName;
}
}
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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.hojat.LibSpringMVCProject.DAO" />
<context:component-scan base-package="com.hojat.LibSpringMVCProject.Service" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/library"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</bean>
<bean id="author" class="com.hojat.LibSpringMVCProject.model.Author"/>
<bean id="authorDAO" class="com.hojat.LibSpringMVCProject.DAO.AuthorDAO" >
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="authorService" class="com.hojat.LibSpringMVCProject.service.AuthorService"/>
错误:
'INFO: Loaded JDBC driver: com.mysql.jdbc.Driver
Feb 24, 2015 1:16:11 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [spring-dispatcher] in context with path [/LibrarySpringMVCProject] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at com.hojat.LibSpringMVCProject.DAO.AuthorDAO.addAuthor(AuthorDAO.java:32)
at com.hojat.LibSpringMVCProject.service.AuthorService.addAuthor(AuthorService.java:40)
at com.hojat.LibSpringMVCProject.controller.AuthorController.addAuthorExecution(AuthorController.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:146)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:277)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)'
答案 0 :(得分:0)
你好,你可以尝试
<property name="url" value="jdbc:mysql://localhost:3306/library"/>
而不是
<property name="url" value="jdbc:mysql://localhost/library"/>
这将是问题。
答案 1 :(得分:0)
几个错误
Auther
。这是错误的。@Service
AuthorService
注释
AuthorDAO
方法中创建addAuthor
的对象。您应该注意,AutherDAO
不应该手动构建,因为该类autowired
中有AutherDAO.AuthorMapper
个对象可用。通过手动构建它们,这些自动布线将不会连接到对象。如果我完全重写了你的服务类,那就是这样的。
@Service
public class AuthorService {
@Autowired
AuthorDAO authorDAO;
public void addAuthor(String authorName) throws Exception {
authorDAO.addAuthor(authorName);
}
}