Jdbc的update空指针异常
@org.junit.Test
public void testUpdate(){
String sql = "insert into student(name,sex) values(?,?)";
jdbcTemplate.update(sql,new Object[]{"小明","男"});
}
===========================================
java.lang.NullPointerException
at Test.testUpdate(Test.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Process finished with exit code -1
正在回答 回答被采纳积分+1
我知道为啥了,因为没有用大括号把那两个语句括起来形成代码块
//大括号括起来的部分代表代码块,每次调用构造方法前执行,都会先执行构造代码块。
{
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
jdbcTemplate = (JdbcTemplate)context.getBean("jdbcTemplate");
}
//每次构造方法执行前如果没有括起来形成代码块就不会执行这部分内容
jdbcTemplate也就自然为null
@org.junit.Test
public void testUpdate(){
String sql = "insert into student(name,sex) values(?,?)";
jdbcTemplate.update(sql,new Object[]{"张飞","男"});
}
这个代码执行时没代码块谁知道jdbcTemplate是干啥的,这部分代码块就是对jdbcTemplate进行对象注入的,注入后才能进行操作好吧。
{
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
jdbcTemplate = (JdbcTemplate)context.getBean("jdbcTemplate");
}
你好同学,如下图所示,你有没有在spring.xml中配置id为jdbcTemplate的bean呢?如过没有建议添加上,或者将spring.xml中的代码也粘贴上来哦,祝学习愉快~
还有这里也包异常了,我写的sql语句检查好多遍了,还是报异常
@org.junit.Test
public void testQuerySimple1(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
JdbcTemplate jdbcTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate");
String sql = "select count(*) form student";
int count = jdbcTemplate.queryForObject(sql,Integer.class);
System.out.println(count);
}
异常信息====================================
Caused by: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'student' at line 1
显示错误的那行代码: int count = jdbcTemplate.queryForObject(sql,Integer.class);
public class Test {
private JdbcTemplate jdbcTemplate;
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
JdbcTemplate jdbcTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate");
}
@org.junit.Test
public void testUpdate(){
String sql = "insert into student(name,sex) values(?,?)";
jdbcTemplate.update(sql,new Object[]{"小明","男"});
}
你好同学,可能是你的jdbcTemplate对象位null,所以会报空指针异常,建议你将你的全部代码粘贴上来,方便老师进行测试,并具有针对性的为你解答,祝学习愉快~
- 参与学习 人
- 提交作业 205 份
- 解答问题 4317 个
Java中非常实用的SSM整合开发内容,从Spring开始,到MyBaits的进阶内容,再到SpringMVC的应用,最后是SSM整合开发案例,逐步深入,助你成长为一名Java工程师!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星