简体   繁体   中英

NullPointerException While Creating Session With Hibernate 4.3.10 FINAL

I have created a simple Member Model Class and trying to retrieve all the members from the DB using Hibernate.
The hibernate.cfg.xml is as 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 the 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>


The Code For List members is as follows :

@SuppressWarnings("deprecation")
public static ArrayList<Member> listMembers(){
    Session sessionNew = null;
    ArrayList<Member> membrArray = new ArrayList<Member>();
    try{
          /* This step will read hibernate.cfg.xml and prepare hibernate for 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 * FROM 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 will happen at this step*/
        sessionNew.flush();
        sessionNew.close();
    }
    return membrArray;

}

Whenever the code is executed and the method is invoked by a calling function then a is thrown at

sessionNew.flush();
sessionNew.close();

Within The finally Block.
What am I doing wrong ?

sessionNew is not initialized as you expect it to be. There seems to be an exception thrown either before its initialized or at the point of initializing it. The code should have run the catch block sysout statement after the exception is thrown. Since Finally block always run, it just gets executed calling flush() on a null object. Please check why sessionNew is not getting initialized. Try to debug and check it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM