当前位置: 首页 > 知识库问答 >
问题:

当我试图访问映射到控制器的页面时,java.lang.NullPointerException

子车灿
2023-03-14
SEVERE: Servlet.service() for servlet [stbam] in context with path [/STBAM] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
    at com.gsimt.stbam.service.UserServiceImpl.listAllUsers(UserServiceImpl.java:16)
    at com.gsimt.stbam.controller.UserController.listUsers(UserController.java:24)
    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.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
package com.gsimt.stbam.controller;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import com.gsimt.stbam.domain.User;
import com.gsimt.stbam.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

     @RequestMapping("/users")
        public String listUsers(Map<String, Object> map) {

            map.put("user", new User());
            map.put("userList", userService.listAllUsers());

            return "users";
        }
}
package com.gsimt.stbam.service;

import java.util.List;

import com.gsimt.stbam.domain.User;
import com.gsimt.stbam.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;

public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    public List<User> listAllUsers() {
        // TODO Auto-generated method stub
        return userDao.listUsers();
    }

    public User getUserById(Integer Id) {
        // TODO Auto-generated method stub
        return userDao.getUserById(Id);
    }

    public void addUser(User user) {
        // TODO Auto-generated method stub
        userDao.addOrUpdateUser(user);
    }

    public void deleteUser(Integer Id) {
        // TODO Auto-generated method stub
        userDao.deleteUser(Id);
    }

}

userdao.java:

package com.gsimt.stbam.dao;

import java.util.List;

import com.gsimt.stbam.domain.User;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.beans.factory.annotation.Autowired;

@Repository
public class UserDaoImpl implements UserDao{

    @Autowired
    private SessionFactory sessionFactory;

    public List<User> listUsers() {
        // TODO Auto-generated method stub
        return (List<User>)sessionFactory.getCurrentSession().createQuery("from User").list();
    }

    public User getUserById(Integer Id) {
        // TODO Auto-generated method stub
        User user = (User) sessionFactory.getCurrentSession().load(User.class, Id);
        return user;
    }

    public void addOrUpdateUser(User user) {
        // TODO Auto-generated method stub
        sessionFactory.getCurrentSession().saveOrUpdate(user);
    }

    public void deleteUser(Integer Id) {
        // TODO Auto-generated method stub
        User user = (User) sessionFactory.getCurrentSession().load(User.class, Id);
        if(user != null){

            sessionFactory.getCurrentSession().delete(user);
        }
    }

}

user.java

package com.gsimt.stbam.domain;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Table;
import javax.persistence.Id;
import javax.persistence.Column;

@Entity
@Table (name = "USER")
public class User implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @Column (name = "ID")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer Id;

    @Column (name = "LOGIN")
    private String login;

    @Column (name = "FIRSTNAME")
    private String firstName;

    @Column (name = "LASTNAME")
    private String lastName;

    @Column (name = "EMAIL")
    private String email;

    @Column (name = "HASH")
    private String hash;

    public Integer getId() {
        return Id;
    }

    public void setId(Integer id) {
        Id = id;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getHash() {
        return hash;
    }

    public void setHash(String hash) {
        this.hash = hash;
    }


}

和我的disparcher上下文stbam-servlet.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: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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <context:annotation-config />
    <context:component-scan base-package="com.gsimt.stbam" />

    <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/configuration.properties" />



    <tx:annotation-driven />

</beans>
<?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: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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">


  <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
        p:driverClassName="com.mysql.jdbc.Driver"
        p:url="jdbc:mysql://localhost:3306/STBAMDB" p:username="root"
        p:password="123456" />


    <!-- to use hibernate 4, we have to use at least spring 3.2.2 The AnnotationSessionFactoryBean

    is not supported anymore and replaced by LocalSessionFactoryBean-->
    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
            <property name="packagesToScan" value="com.gsimt.stbam.domain"></property>
            <property name="hibernateProperties">
                 <value>
                    hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
                    hibernate.hbm2ddl.auto=update
                    hibernate.show_sql=true
                </value>
           </property>    
   </bean>

   <bean id="userDao"
         class="com.gsimt.stbam.dao.UserDaoImpl" />



<bean id="transactionManager" 
  class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

  </beans>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/stbam-service.xml
            /WEB-INF/stbam-data.xml
            /WEB-INF/stbam-security.xml
        </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>stbam</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>detectAllViewResolvers</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>stbam</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

即使我添加了@service和@repositort(“userdao”)

共有1个答案

雍马鲁
2023-03-14

而不是

@Repository
public class UserDaoImpl implements UserDao{

使用

@Repository("userDao")
public class UserDaoImpl implements UserDao{

通过DefualtuserDaoImpl将具有名称userDaoImpl,因此其not autowiring

<import resource="classpath:stbam-data.xml" />
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/stbam-service.xml,
            /WEB-INF/stbam-data.xml,
            /WEB-INF/stbam-security.xml
        </param-value>

</context-param>
 类似资料:
  • 有类似的主题,但它们都使用xml配置文件。我之所以写这个问题,是因为我使用了注释。 我在运行应用程序时遇到问题: 在尝试设置Spring servlet时,获取WARNorg.springframework.web.servlet.PageNotes-未找到HTTP请求与URI的映射 尝试在服务器上运行时出现错误404 以下是我的代码(跳过包和导入): 1)初始化器 2)应用配置 3)控制器 控制

  • 我在Spring绘制地图时遇到了麻烦。我有一个Spring web项目,有用户和数据库。我得到了这个网站。xml: 今年Spring: 这是我的控制器: 我想把所有的请求映射到网址“http://localhost:8080/CRUDWebAppMavenized/users”将打开users.jsp.但我总是只看到错误页面。 IDE告诉我: 警告:org.springframework.web.

  • 我有一个Spring Boot web应用程序,它可以服务于静态页面和基于控制器的页面(ModelAndView)。问题是,控制器可以提供类似于<代码>/{字符串}的服务,而静态页面必须提供<代码>/测试。 问题是控制器映射优先,我需要避免这种情况。如果用户点击测试,则必须将其转发到测试。html静态页面。 我尝试以这种方式使用ViewControlller注册表的order属性,但没有成功: 这

  • 本文向大家介绍iOS 访问容器视图控制器,包括了iOS 访问容器视图控制器的使用技巧和注意事项,需要的朋友参考一下 示例 当视图控制器出现在标签栏控制器中时,您可以像这样访问标签栏控制器: 迅速 目标C 当视图控制器是导航堆栈中的一部分时,您可以按以下方式访问导航控制器: 迅速 目标C            

  • 我是CMIS和AlFresco的新手,当我尝试使用AtomPUB绑定连接到我的AlFresco存储库时遇到了这个错误。我不知道我的问题的来源。除非是功能吗?是我的凭据吗?当我安装它时,我只选择:-AlFresco社区-Solr4 如果我想使用Web服务,我应该怎么做?我应该在我的AlFresco中安装特定的插件吗? 我得到了错误: 我的代码:

  • 我很难弄清楚问题出在哪里。 我认为最简单的方法是使用一个带有请求映射的控制器。 移动应用程序有它自己的身份验证系统,所以访问url需要是开放的/匿名的。 我已经设置了以下内容: 在日志中,当服务器运行时,我会得到以下消息: