当前位置: 首页 > >

java 获取sqlsession_Mybatis中获得SqlSession对象空指针的解决办法!

发布时间:

出现空指针


java.lang.NullPointerException


at com.chun.utils.MybatisUtils.getSqlSession(MybatisUtils.java:26)


at com.chun.dao.UserMapperTest.test(UserMapperTest.java:16)


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.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)


at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)


at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)


MybatisUtils工具类出现问题


private static SqlSessionFactory sqlSessionFactory;


static {


try {


//使用Mybatis第一步 获取sqlSessionFactory对象


String resource = "mybatis-config.xml";


InputStream inputStream = Resources.getResourceAsStream(resource);


//SqlSessionFactory 出现问题


SqlSessionFactory sqlSessionFactory = new


SqlSessionFactoryBuilder().build(inputStream);


} catch (IOException e) {


e.printStackTrace();


}


}


解决


在MybatisUtils工具类中,把


SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);


改为:


sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);


因为sqlSessionFactory已经在上面定义了类中定义了!







相关资源:图书管理系统(Java + Mysql)我第一个完全自己做实训项目



友情链接: 时尚网 总结汇报 幼儿教育 小学教育 初中学习资料网