i have created simple member model class , trying retrieve members db using hibernate.
hibernate.cfg.xml follows :
<?xml version='1.0' encoding='utf-8'?> <!doctype hibernate-configuration public "-//hibernate/hibernate configuration dtd//en" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.sqlserver2008dialect</property> <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.sqlserverdriver</property> <property name="hibernate.connection.url">jdbc:sqlserver://192.168.0.112:1433;databasename=wbsedcl</property> <property name="hibernate.connection.username">sa</property> <property name="hibernate.connection.password">asdf@123</property> <property name="hibernate.connection.autocommit">true</property> <!-- enable hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- disable second-level cache --> <property name="cache.provider_class">org.hibernate.cache.nocacheprovider</property> <property name="show_sql">true</property> <!-- mapping files --> <mapping resource="member.hbm.xml"/> </session-factory> </hibernate-configuration>
code list members follows :
@suppresswarnings("deprecation") public static arraylist<member> listmembers(){ session sessionnew = null; arraylist<member> membrarray = new arraylist<member>(); try{ /* step read hibernate.cfg.xml , prepare hibernate use */ configuration configuration = new configuration(); configuration.configure("hibernate.cfg.xml"); standardserviceregistrybuilder ssrb = new standardserviceregistrybuilder().applysettings(configuration.getproperties()); sessionfactory sessionfactorynew = configuration.buildsessionfactory(ssrb.build()); sessionnew = sessionfactorynew.opensession(); string sql_query ="select * member membr"; query query = sessionnew.createquery(sql_query); for(iterator it=query.iterate();it.hasnext();){ object[] row = (object[]) it.next(); member membr_obj = new member((string)row[0],(string)row[1],(string)row[2],(string)row[3],(string)row[4],(string)row[5],(string)row[6],(string)row[7], (string)row[8],(string)row[9],(string)row[10],(string)row[11],(string)row[12],(string)row[13],(string)row[14],(string)row[15],(string)row[16],(string)row[17], (string)row[18],(string)row[19],(string)row[20],(string)row[21],(string)row[22],(string)row[23],(string)row[24],(string)row[25],(string)row[26], (string)row[27],(string)row[28],(string)row[29],(string)row[30],(string)row[31],(string)row[32],(string)row[33],(string)row[34],(string)row[35], (string)row[36],(string)row[37],(string)row[38],(string)row[39],(string)row[40],(string)row[41],(string)row[42],(string)row[43], (string)row[44],(string)row[45],(string)row[46],(string)row[47],(string)row[48],(string)row[49],(string)row[50],(string)row[51],(string)row[52],(string)row[53]); membrarray.add(membr_obj); } } catch(exception exception){ system.out.println("exception in listmember function in persistencemanager"); exception.printstacktrace(); } finally{ /* actual contact insertion happen @ step*/ sessionnew.flush(); sessionnew.close(); } return membrarray; } whenever code executed , method invoked calling function nullpointerexception thrown @
sessionnew.flush();
sessionnew.close();
within block.
doing wrong ?
sessionnew not initialized expect be. there seems exception thrown either before initialized or @ point of initializing it. code should have run catch block sysout statement after exception thrown. since finally block run, gets executed calling flush() on null object. please check why sessionnew not getting initialized. try debug , check it.
Comments
Post a Comment