How to resolve the error Caused by: java.lang.Nullpointerexception in getSessionFactory()

Asked

Viewed 903 times

1

Is popping this error message for me

inserir a descrição da imagem aqui

Here are the lines that gave error:

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {

            Configuration configuration = new Configuration();
            configuration.configure();

            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                    .applySettings(configuration.getProperties()).build();

             SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

            return sessionFactory;

        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.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

https://github.com/wladyband/Dragaria/tree/master/Drogaria

Here is 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>

Full 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
  • It cannot have been an error on all these lines, so if possible, put the log error to further clarify your doubt.

  • sorry I didn’t put the image,.

  • If possible put your stacktrace in full text format.

  • I just put, please take another look at my post.

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 {
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.