08_ a java.lang.NullPointException error

Source: Internet
Author: User

"Error Code"

 Public classuserdaotest{Privatesqlsessionfactory sqlsessionfactory; //This method executes prior to executing Testfinduserbyid ()@Before Public voidSetUp ()throwsException {//Create Sqlsessionfactory//mybatis configuration fileString resource= "Sqlmapconfig.xml"; //get configuration file streamInputStream inputstream=Resources.getresourceasstream (Resource); //creating a session factory, passing in MyBatis profile information        sqlsessionfactory sqlsessionfactory=NewSqlsessionfactorybuilder (). Build (InputStream); } @Test Public voidTestfinduserbyid ()throwsException {//Create an Userdao objectUserdao Userdao =NewUserdaoimpl (sqlsessionfactory); //methods for calling UserdaoUser User =userdao.finduserbyid (4);    System.out.println (User.tostring ()); }}

"Fix Code"

 Public classuserdaotest{Privatesqlsessionfactory sqlsessionfactory; //This method executes prior to executing Testfinduserbyid ()@Before Public voidSetUp ()throwsException {//Create Sqlsessionfactory//mybatis configuration fileString resource= "Sqlmapconfig.xml"; //get configuration file streamInputStream inputstream=Resources.getresourceasstream (Resource); //creating a session factory, passing in MyBatis profile information        sqlsessionfactory=NewSqlsessionfactorybuilder (). Build (InputStream); } @Test Public voidTestfinduserbyid ()throwsException {//Create an Userdao objectUserdao Userdao =NewUserdaoimpl (sqlsessionfactory); //methods for calling UserdaoUser User =userdao.finduserbyid (4);    System.out.println (User.tostring ()); }}

"Cause analysis"

The @Before method executes before the @test method, copies the previous code directly, and defines the private sqlsessionfactory sqlsessionfactory in the class outside of the two methods;

Two methods are intended to be common, but they have copied the previous code, but ignore the @before will re-new an object,sqlsessionfactory sqlsessionfactory=new Sqlsessionfactorybuilder (). Build (InputStream); The final debug only found the error.

Write this article and remember not to make this mistake!

08_ a java.lang.NullPointException error

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.