Exception

Olá pessoal

Estou desenvolvendo um sistema com struts, hibernate e etc.
Criei uma aquivo para testar a persistencia da classe que mapiei para o hibernate, mas deu ema Exception.

A Exception que deu foi essa:

Exception in thread "main" java.lang.NullPointerException
	at br.gov.mapa.lib.hibernate.HibernateDAO.getSession(HibernateDAO.java:32)
	at br.gov.mapa.lib.hibernate.HibernateDAO.insert(HibernateDAO.java:100)
	at br.gov.mapa.siag.model.SiagFacade.incluirTipoDocumento(SiagFacade.java:63)
	at br.gov.mapa.siag.model.Teste.main(Teste.java:16)

minha classe de teste que criei, para testar a persistênsia:

public class Teste {

    private static String nome= "TEste Teste";
    
    public static void main(String args[]) throws SiagBusinessException {
        
        TipoDocumento td = new TipoDocumento();
        
        td.setDescricaoTipoDocumento(nome);
        //td.setId(new Long(100));
        SiagFacade sg = new SiagFacade();
        sg.incluirTipoDocumento(td);
    }
}

na minha classe de Teste, instanciei a classe SiagFacade , no qual chamei o metodo incluirTipoDocumento():

         public void incluirTipoDocumento(TipoDocumento tipoDocumento) throws SiagBusinessException {
             logger.debug("Iniciando: "+this.getClass().getName()+".incluirTipoDocumento()");
             try {
                 TipoDocumentoDAO dao = new TipoDocumentoDAO();
                 dao.insert(tipoDocumento);
             } catch (DAOException e) {
                 logger.error(e.getMessage()+" em "+this.getClass().getName());
                 throw new SiagBusinessException(e.getMessage(), e);
             }
             logger.debug("Finalizando: "+this.getClass().getName()+".incluirTipoDocumento()");
         }

no metodo incluirTipoDocumento() instanciei a classe TipoDocumentoDAO:

package br.gov.mapa.siag.model;

import br.gov.mapa.lib.hibernate.HibernateDAO;
import br.gov.mapa.lib.util.arquitetura.DAOException;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.ObjectNotFoundException;
import net.sf.hibernate.engine.SessionImplementor;
import net.sf.hibernate.impl.QueryImpl;
import net.sf.hibernate.Session;
import org.apache.log4j.*;

public class TipoDocumentoDAO extends HibernateDAO{
    static private Logger logger = Logger.getLogger("siag");

    /**
     * Permite recuperar uma lista de todos objetos do tipo TipoDocumento
     * @throws DAOException
     * @return Collection contendo objetos TipoDocumento
     * @param
     */
    public Collection findAll() throws DAOException {
        logger.debug("Iniciando: "+this.getClass().getName()+".findAll()");
        try {
            QueryImpl query = new QueryImpl("select tipoDocumento from br.gov.mapa.siag.model.TipoDocumento tipoDocumento order by tipoDocumento.id", (SessionImplementor)getSession());
            logger.debug("Retornando o resultado/fechando: "+this.getClass().getName()+".findAll()");
            return query.list();
        } catch (HibernateException e) {
            logger.error(e.getMessage()+" em "+this.getClass().getName());
            throw new DAOException("Erro ao executar query.", e);
        }
    }

 
 
}

Alguém pode me dizer o que fiz de errado ?

Valew

vixi meo, do jeito q dá nao da pra saber direito…
posta esse metodo getSession do HibernateDAO.

Teu problema está no Hibernate, já tentou fazer o debug e ver como ele processa seu HibernateDAO?
verifica se a session é diferente de null, de repente tem q instancia-la.

Ah beleza moises.trovo,

// 
// Oracle JDeveloper Stub Generated Source
// 
package br.gov.mapa.lib.hibernate;

public class HibernateDAO
	implements br.gov.mapa.lib.util.arquitetura.DAO
{
	// 
	// Constructors
	// 
	public HibernateDAO() { }

	// 
	// Fields
	// 
	private net.sf.hibernate.Session session;

	private static org.apache.log4j.Logger log;

	// 
	// Methods
	// 
	public net.sf.hibernate.Session getSession() { }

	public void remove(java.lang.Object p1) { }

	public java.lang.Object load(java.lang.Class p1, java.io.Serializable p2) { }

	public void update(java.lang.Object p1) { }

	public void insert(java.lang.Object p1) { }

	public java.util.Collection findByNamedQuery(java.lang.String p1, java.util.HashMap p2) { }
}

Blz?

Provavelmente está tentando usar uma variável nula, lembrando que o Hibernate sempre trabalha com objetos, nunca com variáveis primitivas. Verifique se está usando int e troque pela classe Integer, por exemplo. Em alguns sistemas que dei manutenção essa alteração resolveu.

[quote=fbeli]Teu problema está no Hibernate, já tentou fazer o debug e ver como ele processa seu HibernateDAO?
verifica se a session é diferente de null, de repente tem q instancia-la.[/quote]
Boa.