gpt4 book ai didi

spring 数据源中的 java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-11-29 00:12:52 25 4
gpt4 key购买 nike

我在 MySql 中完成了数据库表。我想要 spring 应用程序和数据库之间的连接。例如,我在编辑文本中插入值,它可以保存在数据库(MySql)中但我得到了错误。我是 Spring 的新手,需要帮助。

以下是我的错误日志。

16:05:44,784 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/SpringSample].[mvc-dispatcher]] (http-localhost/127.0.0.1:8080-1) Servlet.service() for servlet mvc-dispatcher threw exception: java.lang.NullPointerException
at com.rxoffice.test.controller.JDBCStudentDAO.findByStudentId(JDBCStudentDAO.java:65) [classes:]
at com.rxoffice.test.controller.Home.hi(Home.java:33) [classes:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.6.0_32]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [rt.jar:1.6.0_32]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [rt.jar:1.6.0_32]
at java.lang.reflect.Method.invoke(Method.java:597) [rt.jar:1.6.0_32]
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176) [spring-web-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426) [spring-webmvc-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414) [spring-webmvc-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790) [spring-webmvc-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719) [spring-webmvc-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644) [spring-webmvc-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549) [spring-webmvc-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) [jboss-servlet-api_3.0_spec-1.0.1.Final.jar:1.0.1.Final]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.1.Final.jar:1.0.1.Final]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.17.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.17.Final.jar:]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.17.Final.jar:]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.17.Final.jar:]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:165) [jboss-as-web-7.1.3.Final.jar:7.1.3.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.17.Final.jar:]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.17.Final.jar:]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.17.Final.jar:]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:372) [jbossweb-7.0.17.Final.jar:]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.17.Final.jar:]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:679) [jbossweb-7.0.17.Final.jar:]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:931) [jbossweb-7.0.17.Final.jar:]
at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_32]

以下是我的文件。

JDBCStudentDAO.java -

 import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

public class JDBCStudentDAO implements StudentDAO {

/*@Autowired
DataSource dataSource;*/
private DataSource dataSource;
JdbcTemplate jdbcTemplate;

public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}


@Override
public void insert(Student student) {
// TODO Auto-generated method stub

String sql = "INSERT INTO student_info " + "(id , name) VALUES (?, ?)";
Connection conn = null;


try {
conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, student.getId());
ps.setString(2, student.getName());
ps.executeUpdate();
ps.close();

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{

if (conn != null) {
try {
conn.close();
} catch (SQLException e) {}
}
}


}

@Override
public Student findByStudentId(int stdId) {
// TODO Auto-generated method stub

String sql = "SELECT * FROM student_info WHERE id = ?";

Connection conn = null;

try {
conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, stdId);
Student student = null;
ResultSet rs = ps.executeQuery();
if (rs.next()) {
student = new Student(
rs.getInt("id"),
rs.getString("name")
);
}
rs.close();
ps.close();
return student;
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {}
}
}
}

最佳答案

正如您在评论中提到的,错误发生在 dataSource.getConnection() - 意思是 dataSource一片空白。您已经注释掉了那个 bean 的 Autowiring ,所以您可能忘记了显式设置它:

/*@Autowired DataSource dataSource;*/
private DataSource dataSource;

public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}

你的相关部分applicationContext.xml文件可能会更清楚地说明问题 - 您可能需要

<context:annotation-config/>
<context:component-scan base-package="com.whatever.jdbc... , more.packages.to.scan,..."/>

或明确的 <property name="dataSource" ref="..." />JDBCStudentDao 上bean XML 配置。

干杯,

关于spring 数据源中的 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24203686/

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