How to solve the error Caused by: java.lang.NullPointerException in getSessionFactory ()

1

This error message is appearing to me

Herearethelinesthatgaveerror:

publicclassHibernateUtil{privatestaticfinalSessionFactorysessionFactory=buildSessionFactory();privatestaticSessionFactorybuildSessionFactory(){try{Configurationconfiguration=newConfiguration();configuration.configure();ServiceRegistryserviceRegistry=newStandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();SessionFactorysessionFactory=configuration.buildSessionFactory(serviceRegistry);returnsessionFactory;}catch(Throwableex){//Makesureyoulogtheexception,asitmightbeswallowedSystem.err.println("Falha ao tentar criar uma SessionFactory." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

@SuppressWarnings("unchecked")
    public List<Fabricante> listar() {
        Session sessao = HibernateUtil.getSessionFactory().openSession();
        List<Fabricante> fabricantes = null;
        try {
            Query consulta = sessao.getNamedQuery("Fabricante.listar");
            fabricantes = consulta.list();
        } catch (RuntimeException ex) {
            throw ex;
        } finally {
            sessao.close();
        }
        return fabricantes;
    }

@Test
    public void listar(){
        FabricanteDAO dao = new FabricanteDAO();
        List<Fabricante> fabricantes = dao.listar();

        for(Fabricante fabricante : fabricantes){

            System.out.println(fabricante);
        }
    }

Can anyone identify the problem?

Here is the repository

link

Here's my Query

@Entity
@Table(name = "tbl_fabricante")
@NamedQueries({ @NamedQuery(name = "Fabricante.listar", query = "SELECT Fabricante from Fabricante fabricante") })

public class Fabricante {

And here the hibernate.cfg.xml :

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">123</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost/db_drogaria</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>


        <property name="hibernate.hbm2ddl.auto">validate</property>


        <property name="show_sql">true</property>
        <property name="format_sql">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.internal.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>


        <mapping class="br.com.drogaria.domain.Fabricante"/>
        <mapping class="br.com.drogaria.domain.Funcionario"/>
        <mapping class="br.com.drogaria.domain.Produto"/>
        <mapping class="br.com.drogaria.domain.Venda"/>
        <mapping class="br.com.drogaria.domain.Item"/>


    </session-factory>

</hibernate-configuration>

Complete stacktrace error.

abr 14, 2015 1:43:46 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
abr 14, 2015 1:43:46 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.8.Final}
abr 14, 2015 1:43:46 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
abr 14, 2015 1:43:46 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
abr 14, 2015 1:43:46 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
abr 14, 2015 1:43:46 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
abr 14, 2015 1:43:46 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
abr 14, 2015 1:43:46 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
abr 14, 2015 1:43:46 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/db_drogaria]
abr 14, 2015 1:43:46 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=root, password=****}
abr 14, 2015 1:43:46 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
abr 14, 2015 1:43:46 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
abr 14, 2015 1:43:47 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
abr 14, 2015 1:43:47 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
abr 14, 2015 1:43:47 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
abr 14, 2015 1:43:48 PM org.hibernate.tool.hbm2ddl.SchemaValidator validate
INFO: HHH000229: Running schema validator
abr 14, 2015 1:43:48 PM org.hibernate.tool.hbm2ddl.SchemaValidator validate
INFO: HHH000102: Fetching database metadata
abr 14, 2015 1:43:48 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: db_drogaria.tbl_fabricante
abr 14, 2015 1:43:48 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [fab_descricao, fab_codigo]
abr 14, 2015 1:43:48 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: db_drogaria.tbl_funcionarios
abr 14, 2015 1:43:48 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [fun_cpf, fun_nome, fun_senha, fun_codigo, fun_funcao]
abr 14, 2015 1:43:48 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: db_drogaria.tbl_itens
abr 14, 2015 1:43:48 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [tbl_produtos_pro_codigo, tbl_vendas_ven_codigo, ite_valor_parcial, ite_quantidade, ite_codigo]
abr 14, 2015 1:43:48 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: db_drogaria.tbl_produtos
abr 14, 2015 1:43:48 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [pro_codigo, tbl_fabrincantes_fab_codigo, pro_descricao, pro_quantidade, pro_preco]
abr 14, 2015 1:43:48 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: db_drogaria.tbl_vendas
abr 14, 2015 1:43:48 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [ven_horario, tbl_funcionarios_fun_codigo, ven_valor_total, ven_codigo]
Falha ao tentar criar uma SessionFactory.java.lang.NullPointerException
    
asked by anonymous 14.04.2015 / 17:30

1 answer

1

The problem has been fixed in the query

@Entity
@Table(name = "tbl_fabricante")
@NamedQueries({ @NamedQuery(name = "Fabricante.listar", query = "SELECT f from Fabricante f") })

public class Fabricante {
}
    
14.04.2015 / 19:15