Java.lang.ExceptionInInizerError

4 respostas
R

Estou fazendo um projeto com jsf e hibernate mas estou tendo o seguinte erro

javax.faces.el.EvaluationException: java.lang.ExceptionInInitializerError

alguem poderia me ajudar por favor???

Obrigado!!!

4 Respostas

J

Você não tem por acaso um atributo estatico que está sendo lançada uma exceção quando você tenta inicializar ele?

ex.:

public class MinhaClasse{
   public static int teste = calculaTeste();

   private static int calculaTeste(){
      throw new RuntimeException("Erro");
   }
}
R

não, olha só as minhas classes e jsp's

index.jsp

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Folha de Pagamento WEB - CRECI/SP</title>
    </head>
    
   <f:view>
        <h:form>
            <center>
<br>
                    <h:commandButton action="#{beneficios.buscar}" value="Beneficios"/>
                
                </h3>
            </center>
        </h:form>
    </f:view>    
</html>

meu bean:

package br.com.creci.fp.hibernate;

import javax.faces.context.FacesContext;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.Session;

import br.com.creci.fp.dao.BeneficiosDAO;

import org.hibernate.HibernateException;

@Entity @Table(name="Fol_Tabela_Beneficios")
public class Beneficios implements Serializable {

	private Long id;
	private String descricao;
	private Long funcionario;
	private Long dependente;
	
    private String result_alterar = "";
    private String result_inserir = "";
    
    public static final String SUCESSO_ALTERACAO = "success";
    public static final String FALHA_ALTERACAO = "failure";
    public static final String SUCESSO_INSERCAO = "success";
    public static final String FALHA_INSERCAO = "failure";
    public static final String BUSCA_VALIDA = "success";
    public static final String BUSCA_INVALIDA = "failure";    
    
    @Column(name="Fol_BenDependente")
    public Long getDependente() {
		return dependente;
	}
	public void setDependente(Long dependente) {
		this.dependente = dependente;
	}
	@Column(name="Fol_Ben_Desc")
	public String getDescricao() {
		return descricao;
	}
	public void setDescricao(String descricao) {
		this.descricao = descricao;
	}
	@Column(name="Fol_Ben_Funcionario")
	public Long getFuncionario() {
		return funcionario;
	}
	public void setFuncionario(Long funcionario) {
		this.funcionario = funcionario;
	}
	@Id
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}    
    
    HibernateUtil factory = new HibernateUtil();
    Session session = factory.getSession();
    BeneficiosDAO dao = new BeneficiosDAO(session);
    
    public String buscar() throws SQLException {
        String result_busca = BUSCA_INVALIDA;
        System.out.println("Llegó aqui");
        
		try {
			List<Beneficios> lista = dao.listaTudo();
			if (lista.equals("")) {
				result_busca = BUSCA_INVALIDA;
            }else {

            	id 	 		 = this.getId();
	            descricao  	 = this.getDescricao(); 
	            funcionario  = this.getFuncionario(); 
	            dependente   = this.getDependente();            	
	            result_busca = BUSCA_VALIDA;                
            }
		}catch(org.hibernate.ObjectNotFoundException e) {
			System.out.println("Erro ou não existe!");
			result_busca = BUSCA_INVALIDA;
		}        
        
        return result_busca;
    }
}

página que mostraria o resultado(se estivesse funcionando hehehe):

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<html>
	<head>
		<title>Beneficios</title>
	</head>
	<body>

		<f:view>
			<center><strong><h1>Tabela de Beneficios</h1></strong></center>
        <h:form>
            <table border>
                <tr>
					<td>Ítem</td>
                    <td>Descrição</td>
                    <td>Funcionário</td>
                    <td>Dependente</td>
                </tr>
                
                <tr>
                <td><input type="checkbox" value="ON"/></td>	
                    <td>
                    	<h:outputLink value="/insereAlteraBeneficios.jsp">
                    		<f:verbatim>
		                        <h:outputText value="#{beneficios.descricao}">
    		                    </h:outputText>
    		                </f:verbatim>
    		            </h:outputLink>
    	                    
                    </td>

                    <td>
                    	<h:outputLink value="">
                    		<f:verbatim>
		                        <h:outputText value="#{beneficios.funcionario}">
    		                    </h:outputText>
    		                </f:verbatim>
    		            </h:outputLink>
                    </td>

                    <td>
                    	<h:outputLink value="">
                    		<f:verbatim>
	    	                	<h:outputText value="#{beneficios.dependente}">
    	    	            	</h:outputText>
    	    	           	</f:verbatim>
    	    	        </h:outputLink>
                    </td>
                </tr>
            </table>
            <p>
                <h:commandButton value="Insere" action="insereAlteraBeneficios.jsp"/>
            </p>
            
        </h:form>
        <br>
        <h:outputLink value="index.jsf">
            <f:verbatim>voltar</f:verbatim>
        </h:outputLink>			
		</f:view>
	</body>	
</html>

HibernateUtil:

package br.com.creci.fp.hibernate;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
	private static SessionFactory factory;
	
	static {
		try {
			factory = new Configuration().configure().buildSessionFactory();
		}catch(Throwable ex) {
			throw new ExceptionInInitializerError(ex);
		}
	}
	

	public Session getSession() throws HibernateException {
		return factory.openSession();
	}
}

meu cfg.xml:

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

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
        <property name="connection.url">jdbc:microsoft:sqlserver://base2:1433;DatabaseName=creci_corp_des</property>
        <property name="connection.username">teste</property>
        <property name="connection.password">teste</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.SQLServerDialect</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>

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

		<mapping package="br.com.fp.hibernate"/>
		<mapping class="br.com.fp.hibernate.Beneficios"/>
		
	
    </session-factory>

</hibernate-configuration>

meu faces-config:

<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
                              "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
<faces-config>
 <managed-bean>
  <managed-bean-name>inssDB</managed-bean-name>
  <managed-bean-class>br.com.creci.fp.InssDB</managed-bean-class>
  <managed-bean-scope>session</managed-bean-scope>
 </managed-bean>
 <managed-bean>
  <managed-bean-name>beneficios</managed-bean-name>
  <managed-bean-class>br.com.creci.fp.hibernate.Beneficios</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
 </managed-bean>
 <navigation-rule>
  <from-view-id>/index.jsp</from-view-id>
  <navigation-case>
   <from-outcome>success</from-outcome>
   <to-view-id>/beneficios.jsp</to-view-id>
  </navigation-case>
  <navigation-case>
   <from-outcome>failure</from-outcome>
   <to-view-id>/falha_busca.jsp</to-view-id>
  </navigation-case>
 </navigation-rule>
</faces-config>

Não consigo saber o q pode estar errado
Estou a tarde inteira nisso
Valeu

J

Provavelmente é aí…

static { try { factory = new Configuration().configure().buildSessionFactory(); }catch(Throwable ex) { throw new ExceptionInInitializerError(ex); } }

R

eu tentei trocar, coloquei o hibernate.properties, em vez do cfg.xml e mudei o HibernateUtil assim:

static { AnnotationConfiguration cfg = new AnnotationConfiguration(); cfg.addAnnotatedClass(Beneficios.class); factory = cfg.buildSessionFactory(); }

sabe ? anotar as classes por aqui mesmo, sem usar o cfg.xml
mas dá a mesma coisa

Tenho um outro projetinho de exemplo q está bem parecido com este e dá td certinho

Criado 12 de setembro de 2006
Ultima resposta 12 de set. de 2006
Respostas 4
Participantes 2