The essence of the exception is clear, a little and inconvenient to handle such a minor misunderstanding, but the fact remains. Need your help.

exception org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException org.springframework.web.servlet.FrameworkServlet.processRequest (FrameworkServlet.java:973) org.springframework.web.servlet.FrameworkServlet.doGet (FrameworkServlet.jp.j.j. HttpServlet.service (HttpServlet.java:622) org.springframework.web.servlet.FrameworkServlet.service (FrameworkServlet.java:837) javax.servlet.http.HttpServlet.service (HttpServlet byVtrch.ch. websocket.server.WsFilter.doFilter (WsFilter.java:52)

root cause java.lang.NullPointerException ua.controller. DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) click.. G ho ing.... 97.... Ava ava. Ava ava ava ava ava ava ava ava ava ava ava ava ava Method Method Method Method Method Method Method 97 97. web.methodtech.com method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod (RequestMappingHandlerAdapter.java:749) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal (RequestMappingHandread.andquestMappingHandlerAdapter.handleInternal (RequestMappingHandread.andquestMappingHandlerAdapter.handleInternal (RequestMappingHandread.andquestMappingHandlerAdapter.handleInternal (RequestMappingHandle.dequest 89) org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle (AbstractHandlerMethodAdapter.java:83) org.springframework.web.servlet.DispatcherServlet.doDispatch (DispatcherServlet.e. DispatcherServlet.doService (DispatcherServlet.java:870) org.springframework. http.HttpServlet.service (HttpServlet.java:622) org.springframework.web.servlet.FrameworkServlet.service (FrameworkServlet.java:837) javax.servlet.http.HttpServlet.service (HttpServlet.java:729) org.apache. tomcat.websocket.server.WsFilter.doFilter (WsFilter.java:52)

Main classes

servletController-servlet.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:security="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd"> <mvc:annotation-driven/> <tx:annotation-driven transaction-manager="txManager"/> <context:component-scan base-package="ua"/> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/webapp/jsp"/> <property name="suffix" value=".jsp"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="packagesToScan" value="ua.model"/> <property name="hibernateProperties"> <props> <prop key="hibernate.hbm2ddl.auto">create</prop> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/postschema"/> <property name="username" value="root"/> <property name="password" value="peroser12"/> </bean> </beans> 

Controller

 @Controller public class SSController { Logger logger = Logger.getLogger(SSController.class); private PostDAO postDAO; private CategoryDAO categoryDAO; @RequestMapping (value = "/index", method = RequestMethod.GET) public ModelAndView allList (){ ModelAndView modelAndView = new ModelAndView(); List<Post> posts = postDAO.getAll(); List<Category> categories = categoryDAO.getAll(); modelAndView.addObject("posts", posts); modelAndView.addObject("categories", categories); modelAndView.setViewName("posts"); return modelAndView; } } 

DAOImpl

 public abstract class AbstractDAOImplementation<G> implements AbstractDao<G> { @Autowired @Qualifier(value = "sessionFactory") private SessionFactory sessionFactory; @Transactional @Override public void create(G entity) { Session session = sessionFactory.openSession(); session.beginTransaction(); session.save(entity); session.getTransaction().commit(); session.close(); } @Transactional @Override public void delete(G entity) { Session session = sessionFactory.openSession(); session.beginTransaction(); session.delete(entity); session.getTransaction().commit(); session.close(); } @Transactional @Override public G edit(G entity) { Session session = sessionFactory.openSession(); session.beginTransaction(); session.saveOrUpdate(entity); session.getTransaction().commit(); session.close(); return entity; } @Transactional @Override public G getById(long id) { Session session = sessionFactory.openSession(); session.beginTransaction(); G entity = getEntityById(session, id); session.getTransaction().commit(); session.close(); return entity; } @Transactional @Override public List<G> getAll() { Session session = sessionFactory.openSession(); session.beginTransaction(); List<G> result = getAllEntity(session); return result; } public abstract G getEntityById (Session session, long id); public abstract List<G> getAllEntity(Session session); } 

PostDAO

 public class PostDAO extends AbstractDAOImplementation<Post> { @Override public Post getEntityById(Session session, long id) { Post result = (Post) session.createQuery("from Post where id="+id); return result; } @Override public List<Post> getAllEntity(Session session) { List<Post> result = session.createQuery("from Post ").list(); return result; } } 

Post.java

 @Entity @Table(name = "post") public class Post { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private long id; @Column(name = "title") private String title; @Column(name = "summary") private String summary; @Column(name = "body") private String body; @ManyToOne(cascade = CascadeType.ALL) @JoinColumn private Category category; public Post() { } public Post(long id, String title, String summary, String body, Category category) { this.id = id; this.title = title; this.summary = summary; this.body = body; this.category = category; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getSummary() { return summary; } public void setSummary(String summary) { this.summary = summary; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } public Category getCategory() { return category; } public void setCategory(Category category) { this.category = category; } @Override public String toString() { return "Post{" + "title='" + title + '\'' + ", summary='" + summary + '\'' + ", body='" + body + '\'' + ", category=" + category + '}'; } } 

There are still a couple of classes of models, but they are no different from Post.java.

  • Mark the postDAO and categoryDAO fields in SSController with the SSController annotation, and you are very likely to be happy. - fori1ton
  • You have removed the komenty in the raws, and the stackrays are still from the version with the comments), this is if the previous did not help. - Webaib

2 answers 2

Try marking the DAO with the @Repository(name="abstractImpl") annotation @Repository(name="abstractImpl")

After this, add @Autowired annotations in the controller, if you will again throw out these exceptions, add @Qualifier(name="abstractImpl") to the DAO in the controller.

But besides this, the controller should not work with DAO directly. It should work with the service interface, which in turn is already working with the DAO . I.e:

 @Service public class AbstractServiceImpl implements AbstractService { @Autowired private AbstractDao dao; @Transactional @Override public List<G> getAll() { return this.abstractDao.getAll(); } } 

After that you write to the controller:

 @Autowired private AbstractService service; 

All classes and services are marked with their annotations. And so you have Tao is just a class that does not exist anywhere, but works with the base if you make it a direct instance.

  • I'm going to try now. There is an assumption, personal, can the reason be in an incorrectly initialized Session Factory bean? Used the old way of hml configuration, then the export changed to "stack overflow" - Dmitriy Smirnov
  • @ Dmitry is your personal project or worker? if personal, my advice to you is to connect the Entity Manager . Many times easier and more usable. - raviga
  • @ trembling11 yes, personal. And I'm new. I really want to go to IT. Here I start small. I plan to attach the project to the summary. Thanks for the advice. I get to know - Dmitriy Smirnov
  • @Dmitry, if the answer came up to you, you can mark it as useful. If, then write. - raviga

Your objects

 private PostDAO postDAO; private CategoryDAO categoryDAO; 

in the controller are not initialized at all and are null .

If dao-classes are correctly marked with annotations (which I did not see in the code given by you), then it is enough to add @Autowired annotations over the fields categoryDAO and postDAO

  • I did not describe the dao classes in the hml. Now I initialized the DAO classes, the exception remains. exception org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException java.lang.NullPointerException ua.dao.implementation.AbstractDAOImplementation.getAll(AbstractDAOImplementation.java:63) ua.controller.SSController.allList(SSController.java:30) exception org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException java.lang.NullPointerException ua.dao.implementation.AbstractDAOImplementation.getAll(AbstractDAOImplementation.java:63) ua.controller.SSController.allList(SSController.java:30)
  • @ Dmitry, that is, the spring does not know that these are bins? It is necessary to mark DAO classes with annotations too. If you don’t understand how it works, then briefly and in the simplest case like this: there are Component , Repository , Controller annotations, class objects marked with such annotations will be created with the help of empty constructors and placed in the container of the spring. In order for the desired object from the container to fall into the field of another class (marked with one of the annotations above), you must mark the field with the annotation @Autowired - iksuy