执行存储过程时获取NPE

时间:2019-07-08 04:48:29

标签: java spring-boot spring-data-jpa spring-data jdbctemplate

我正在尝试使用simpleJdbcCall执行一个简单的过程,并且它返回null。

这是我的程序

CREATE DEFINER=`root`@`localhost` PROCEDURE `user_details_procedure`(IN id integer)
BEGIN
 Select name from userdetails where user_Id = id;
END

select * from userdetails

user_Id name   age      city          country   
1   Sam 35  Panama City USA
2   Ronaldo 45  Rio Brazil

POJO

  public class Placeholder {

    private String firstName;

    public String getFirstName() {
        return firstName;
    }

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

    public Placeholder(String firstName) {
        this.firstName = firstName;
    }

    public Placeholder() {
    }

}

@Repository
    public class SomeDao {

        private SimpleJdbcCall procReadActor;

        public void setDataSource(DataSource dataSource) {
            this.procReadActor = new SimpleJdbcCall(dataSource).withProcedureName("user_details_procedure");
        }

        public Placeholder getName(int i) {
            SqlParameterSource in = new MapSqlParameterSource().addValue("user_Id", i);
            Map<String, Object> out = procReadActor.execute(in);
            Placeholder actor = new Placeholder();
            actor.setFirstName((String) out.get("name"));
            return actor;
        }

控制器

@RestController
public class OneController {
@Autowired
    private static SomeDao someDao;

    @Autowired
    private UserDetailsService userDetailsService;

    @GetMapping("/getname/{id}")
    public Placeholder getname(@PathVariable("id") Integer id) {
        return someDao.getName(1);
    }
}

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/demo
spring.datasource.username=root
spring.datasource.password=test
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

spring.datasource.continueOnError=true
spring.main.allow-bean-definition-overriding=true

我收到以下错误:  http://localhost:8080/getname/1

2019-07-08 00:31:48.125  INFO 16500 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 6 ms
2019-07-08 00:31:48.131 ERROR 16500 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null
    at com.example.demo.OneController.getname(OneController.java:21) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_212]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_212]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_212]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_212]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) ~[spring-webmvc-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892) ~[spring-webmvc-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) ~[spring-webmvc-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039) ~[spring-webmvc-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897) ~[spring-webmvc-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) ~[spring-webmvc-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.19.jar:9.0.19]

我已经测试了数据源连接。

[
   {
    id: 1,
    name: "Sam",
    age: 35,
    city: "Panama City",
    country: "USA"
   },
   {
    id: 2,
    name: "Ronaldo",
    age: 45,
    city: "Rio",
    country: "Brazil"
   }
]

更新

2019-07-08 01:03:34.878 ERROR 16500 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null
    at com.example.demo.SomeDao.getName(SomeDao.java:22) ~[classes/:na]
    at com.example.demo.SomeDao$$FastClassBySpringCGLIB$$fa8c1124.invoke(<generated>) ~[classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749) ~[spring-aop-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.1.7.RELEASE.jar:5.1.7.RELEASE]

我必须添加一些内容以避免堆栈错误(看起来您的帖子大部分是代码...)

0 个答案:

没有答案
相关问题