-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: nullPointerException when building the sessionFactory
PostPosted: Sat Aug 06, 2005 11:00 am 
Newbie

Joined: Sat Aug 06, 2005 10:25 am
Posts: 3
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 2.1.8

Mapping documents:
This one is related to Concursos class
Concursos.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="org.hibernate.entregar">
<class name="Concursos" table="concurso">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="descripcion" column="descripcion" type="string"/>
<property name="acronimo" column="acronimo" type="string"/>
<bag name="restricciones" lazy="true" inverse="true" >
<key column="id"/>
<one-to-many class="Restricciones"/>
</bag>

</class>

</hibernate-mapping>

This one is related to class Restricciones
Restricciones.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="org.hibernate.entregar">
<class name="Restricciones" table="opciones">
<id name="id" column="id">
<generator class="native"/>
</id>
<many-to-one name="concurso" column="concurso_ID" not-null="true" />
<property name="opcion" column="opcion" not-null="true" type="string"/>
</class>
</hibernate-mapping>



Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:


[java] 15:34:42,615 INFO Environment:542 - using java.io streams to persis
t binary types
[java] 15:34:42,615 INFO Environment:543 - using CGLIB reflection optimize
r
[java] 15:34:42,615 INFO Environment:572 - using JDK 1.4 java.sql.Timestam
p handling
[java] 15:34:42,625 INFO Configuration:351 - Mapping resource: org/hibernate/entregar/Concursos.hbm.xml
[java] 15:34:43,346 INFO Binder:229 - Mapping class: org.hibernate.entregar.Concursos -> concurso
[java] 15:34:43,486 INFO Configuration:351 - Mapping resource: org/hibernate/entregar/Restricciones.hbm.xml
[java] 15:34:43,526 INFO Binder:229 - Mapping class: org.hibernate.entregar.Restricciones -> opciones
[java] 15:34:43,536 INFO Configuration:641 - processing one-to-many association mappings
[java] 15:34:43,546 INFO Binder:1181 - Mapping collection: org.hibernate.entregar.Concursos.restricciones -> opciones
[java] 15:34:43,546 INFO Configuration:650 - processing one-to-one association property references
[java] 15:34:43,546 INFO Configuration:675 - processing foreign key constraints
[java] 15:34:43,596 INFO Dialect:86 - Using dialect: net.sf.hibernate.dialect.MySQLDialect
[java] 15:34:43,606 INFO SettingsFactory:70 - Maximim outer join fetch depth: 1
[java] 15:34:43,606 INFO SettingsFactory:74 - Use outer join fetching: true
[java] 15:34:43,616 INFO DriverManagerConnectionProvider:42 - Using Hibernate built-in connection pool (not for production use!)
[java] 15:34:43,616 INFO DriverManagerConnectionProvider:43 - Hibernate connection pool size: 1
[java] 15:34:43,636 INFO DriverManagerConnectionProvider:77 - using driver: org.gjt.mm.mysql.Driver at URL: jdbc:mysql:///prueba
[java] 15:34:43,636 INFO DriverManagerConnectionProvider:78 - connection properties: {user=shevek, password=anarres}
[java] 15:34:43,646 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
[java] 15:34:44,357 INFO SettingsFactory:114 - Use scrollable result sets: true
[java] 15:34:44,357 INFO SettingsFactory:117 - Use JDBC3 getGeneratedKeys(): true
[java] 15:34:44,357 INFO SettingsFactory:120 - Optimize cache for minimal puts: false
[java] 15:34:44,357 INFO SettingsFactory:129 - Query language substitution s: {no='N', true=1, yes='Y', false=0}
[java] 15:34:44,367 INFO SettingsFactory:140 - cache provider: net.sf.hibernate.cache.EhCacheProvider
[java] 15:34:44,377 INFO SettingsFactory:152 - query cache factory: net.sf.hibernate.cache.StandardQueryCacheFactory
[java] 15:34:44,377 INFO Configuration:1130 - instantiating and configuring caches
[java] 15:34:44,657 INFO SessionFactoryImpl:119 - building session factory
[java] java.lang.NullPointerException
[java] at org.hibernate.entregar.gui.VentanaServidorConcursos.<init>(Ve
ntanaServidorConcursos.java:44)
[java] at org.hibernate.entregar.gui.VentanaServidorConcursos.main(Vent
anaServidorConcursos.java:381)


Name and version of the database you are using: MySQL 4.0.15

The generated SQL (show_sql=true): It does not generate SQL

Debug level Hibernate log excerpt:

Hello:
Iam trying to run an small example. There are two classes mapped with two different tables. In the first one (Concursos), there is a property that is a list of objects from the other class. That is mapped in this way:
<bag name="restricciones" lazy="true" inverse="true" >
<key column="id"/>
<one-to-many class="Restricciones"/>
</bag>
In the other class (Restricciones), there's a property that is an object from the other class. The mapping is this:
<many-to-one name="concurso" column="concurso_ID" not-null="true" />

As you can see in the trace, when hibernate is building the sessionFactory it crashes with a "nullPointerException"


Top
 Profile  
 
 Post subject: Re: nullPointerException when building the sessionFactory
PostPosted: Sat Aug 06, 2005 12:18 pm 
Regular
Regular

Joined: Thu Dec 02, 2004 7:11 am
Posts: 85
shevek wrote:
[java] 15:34:44,657 INFO SessionFactoryImpl:119 - building session factory
[java] java.lang.NullPointerException
[java] at org.hibernate.entregar.gui.VentanaServidorConcursos.<init>(Ve
ntanaServidorConcursos.java:44)
[java] at org.hibernate.entregar.gui.VentanaServidorConcursos.main(Vent
anaServidorConcursos.java:381)
[/b]

Name and version of the database you are using: MySQL 4.0.15

The generated SQL (show_sql=true): It does not generate SQL

Debug level Hibernate log excerpt:

Hello:
Iam trying to run an small example. There are two classes mapped with two different tables. In the first one (Concursos), there is a property that is a list of objects from the other class. That is mapped in this way:
<bag name="restricciones" lazy="true" inverse="true" >
<key column="id"/>
<one-to-many class="Restricciones"/>
</bag>
In the other class (Restricciones), there's a property that is an object from the other class. The mapping is this:
<many-to-one name="concurso" column="concurso_ID" not-null="true" />

As you can see in the trace, when hibernate is building the sessionFactory it crashes with a "nullPointerException"


I see from stack trace, that NPE is rised in your code, not in Hibernate.
Show us full source of VentanaServidorConcursos class - problem is here.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 08, 2005 6:08 am 
Newbie

Joined: Sat Aug 06, 2005 10:25 am
Posts: 3
I think the crash is when I create an object GestorConcursos, and in this class is where I build the session factory and make the queries and the inserts. The code from VentanaServidorConcursos is this:

package org.hibernate.entregar.gui;

import javax.swing.*;
import java.awt.*;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.List;
import java.util.Iterator;
import org.hibernate.entregar.*;

/**

* Clase que representa la ventana del Servidor de mensajes.

*/

public class VentanaServidorConcursos extends JFrame{

private Vector concursos;
private GestorConcursos gc;
/** Creates a new instance of VentanaServidorConcursos */

public VentanaServidorConcursos()
{
initComponents();
concursos = new Vector();
this.crearGC();
concursosLista = new DefaultListModel();
listaConcursos.setModel(concursosLista);
restricLista = new DefaultListModel();
listaRestric.setModel(restricLista);
concursos= gc.obtenerConcursos();
this.cargarConcursos(concursos);
}

private void initComponents() {

titulo = new javax.swing.JLabel();

panelTitulo = new javax.swing.JPanel();

listaConcursos = new javax.swing.JList();

panelConcursos = new javax.swing.JPanel();

panelListaC = new javax.swing.JPanel();

scrollConcursos = new javax.swing.JScrollPane();

panelListaR = new javax.swing.JPanel();

listaRestric = new javax.swing.JList();

scrollRestric = new javax.swing.JScrollPane();

panelControlC = new javax.swing.JPanel();

tablaPestañas = new javax.swing.JTabbedPane();

panelROpciones = new javax.swing.JPanel();

jLabel2 = new javax.swing.JLabel();

opciones = new javax.swing.JTextField();

botonOpciones = new javax.swing.JButton();

panelRMalsonantes = new javax.swing.JPanel();

jLabel1 = new javax.swing.JLabel();

palabrasNoPermitidas = new javax.swing.JTextField();

botonNoPermitidas = new javax.swing.JButton();

panelBotonesC = new javax.swing.JPanel();

jLabel5 = new javax.swing.JLabel();

acronimo = new javax.swing.JTextField();

jLabel4 = new javax.swing.JLabel();

descripcion = new javax.swing.JTextField();

botonNuevoC = new javax.swing.JButton();

botonSalir = new javax.swing.JButton();

panelBoton = new javax.swing.JPanel();

panelCentral = new javax.swing.JPanel();



setTitle("Servidor Concursos - (CORBA)");

setName("VentanaConcursos");

setResizable(false);

addWindowListener(new java.awt.event.WindowAdapter() {

public void windowClosing(java.awt.event.WindowEvent evt) {

exitForm(evt);

}

});



titulo = new JLabel("GESTIÓN DE CONCURSOS");

titulo.setHorizontalAlignment(SwingConstants.CENTER);

titulo.setForeground(Color.yellow);

titulo.setFont(new Font("Arial", Font.BOLD, 12));

panelTitulo.setBackground(new Color(102, 102, 153));

panelTitulo.setBorder(BorderFactory.createLineBorder(Color.white));

panelTitulo.add(titulo, null);





panelListaC.setBorder(new javax.swing.border.TitledBorder(new javax.swing.border.EtchedBorder(), "Concursos"));

panelListaC.setLayout(new java.awt.BorderLayout());



scrollConcursos.setPreferredSize(new java.awt.Dimension(200, 150));

listaConcursos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

scrollConcursos.setViewportView(listaConcursos);



panelListaC.add(scrollConcursos, java.awt.BorderLayout.CENTER);





panelListaR.setBorder(new javax.swing.border.TitledBorder(new javax.swing.border.EtchedBorder(), "Restricciones"));

panelListaR.setLayout(new java.awt.BorderLayout());



scrollRestric.setPreferredSize(new java.awt.Dimension(200, 150));

//listaRestric.setSelectionMode(javax.swing.ListSelectionModel.);

scrollRestric.setViewportView(listaRestric);



panelListaR.add(scrollRestric, java.awt.BorderLayout.CENTER);



panelConcursos.add(panelListaC);

panelConcursos.add(panelListaR);



panelControlC.setLayout(new java.awt.BorderLayout());



panelControlC.setBorder(new javax.swing.border.TitledBorder(new javax.swing.border.TitledBorder(""), "Configuraci\u00f3n de Concursos"));



jLabel2.setText("Opciones: ");

panelROpciones.add(jLabel2);



opciones.setColumns(15);

panelROpciones.add(opciones);



botonOpciones.setText("A\u00f1adir");

botonOpciones.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

botonOpcionesActionPerformed(evt);

}

});



panelROpciones.add(botonOpciones);



tablaPestañas.addTab("Opciones", panelROpciones);



jLabel1.setText("Palabras: ");

panelRMalsonantes.add(jLabel1);



palabrasNoPermitidas.setColumns(15);

panelRMalsonantes.add(palabrasNoPermitidas);



botonNoPermitidas.setText("A\u00f1adir");

botonNoPermitidas.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

botonPalabrasNoPermitidasActionPerformed(evt);

}

});





panelRMalsonantes.add(botonNoPermitidas);



tablaPestañas.addTab("No Permitidas", panelRMalsonantes);



panelControlC.add(tablaPestañas, java.awt.BorderLayout.SOUTH);



jLabel5.setText("Acron:");

panelBotonesC.add(jLabel5);



acronimo.setColumns(3);

panelBotonesC.add(acronimo);



jLabel4.setText("Desc:");

panelBotonesC.add(jLabel4);



descripcion.setColumns(10);

panelBotonesC.add(descripcion);



botonNuevoC.setText("Nuevo");

botonNuevoC.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

botonNuevoConcursoActionPerformed(evt);

}

});



botonSalir.setText("Parar servidor de concursos");

botonSalir.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

botonSalirActionPerformed(evt);

}

});



listaConcursos.addListSelectionListener(new javax.swing.event.ListSelectionListener()

{

public void valueChanged(javax.swing.event.ListSelectionEvent evt) {

listaConcursosValueChanged(evt);

}



});





panelBoton.add(botonSalir);



panelBotonesC.add(botonNuevoC);



panelControlC.add(panelBotonesC, java.awt.BorderLayout.CENTER);



panelCentral.setLayout(new java.awt.BorderLayout());

panelCentral.add(panelConcursos, java.awt.BorderLayout.NORTH);

panelCentral.add(panelControlC, java.awt.BorderLayout.SOUTH);



getContentPane().setLayout(new java.awt.BorderLayout());



getContentPane().add(panelCentral, java.awt.BorderLayout.CENTER);



getContentPane().add(panelBoton, java.awt.BorderLayout.SOUTH);



getContentPane().add(panelTitulo, java.awt.BorderLayout.NORTH);



pack();

}







private void crearGC()

{

try

{

gc= new GestorConcursos();

}



catch (Exception e)

{

}

}



/**

* Metodo que se ejecuta cuando se pulsa el boton "Nuevo".

* Da de alta un nuevo concurso.

* @param evt Evento generado al pulsar el boton "Nuevo".

*/

private void botonNuevoConcursoActionPerformed(java.awt.event.ActionEvent evt)

{//GEN-FIRST:event_botonNuevoConcursoActionPerformed

this.nuevoConcurso(acronimo.getText(), descripcion.getText());



}//GEN-LAST:event_botonNuevoConcursoActionPerformed



/**

* Metodo que se ejecuta cuando se pulsa el boton "Añadir".

* Añade restriciones del tipo palabra no permitida al concurso

* seleccionado en la lista.

* @param evt Evento generado al pulsar el boton "Añadir".

*/

private void botonPalabrasNoPermitidasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonPalabrasNoPermitidasActionPerformed



}//GEN-LAST:event_botonPalabrasNoPermitidasActionPerformed



/**

* Metodo que se ejecuta cuando se pulsa el boton "Añadir".

* Añade restriciones del tipo opcion al concurso

* seleccionado en la lista.

* @param evt Evento generado al pulsar el boton "Añadir".

*/

private void botonOpcionesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonOpcionesActionPerformed



}//GEN-LAST:event_botonOpcionesActionPerformed



/**

* Metodo que se ejecuta cuando se selecciona un concurso de la lista.

* @param e Evento generado seleccionar un concurso de la lista de concursos.

*/

private void listaConcursosValueChanged(javax.swing.event.ListSelectionEvent e) {



}



/**

* Metodo que se ejecuta cuando se pulsa el boton "Parar Servidor de Concursos".

* Cierra la ventana.

* @param evt Evento generado al pulsar el boton "Parar ervidor de Concursos".

*/

private void botonSalirActionPerformed(java.awt.event.ActionEvent evt){

try

{

gc.desconectar();

System.exit(0);

}

catch (Exception e)

{



}

}



/** Exit the Application */

private void exitForm(java.awt.event.WindowEvent evt) {

try

{

gc.desconectar();

System.exit(0);

}

catch (Exception e)

{



}

}



/**

* Metodo que da de alta un nuevo concurso.

* @param acronimo Acronimo del concurso.

* @param descripcion Nombre del concurso.

*/

private void nuevoConcurso(String acronimo, String descripcion)

{



try

{

gc.insertarConcurso(2, acronimo, descripcion);

}



catch (Exception e)

{

}

}



/**

* Método que da de alta una restricción del tipo palabras no permitidas

* para un concurso seleccionado.

* @param palabras Vector con las palabras no permitidas.

* @param concurso Concurso al que se añaden las restricciones.

*/

public void añadirRestriccionPalabrasNoPermitidas(Vector palabras, Concursos concurso){



}



/**

* Método que da de alta una restricción del opción

* para un concurso seleccionado.

* @param opciones Vector con las opciones permitidas.

* @param concurso Concurso al que se añaden las restricciones.

*/

public void añadirRestriccionOpcionesPermitidas(Vector opciones, Concursos concurso){



}



/**

* Método que separa una cadena de texto compuesta por varias palabras

* separadas por ;.

* @param frase cadena de texto a separar.

* @return Vector con las palabras que se han obtenido al fraccionar la cadena

* de texto.

*/

private Vector separarPalabras(String frase){

Vector v = new Vector();

StringTokenizer st = new StringTokenizer(frase, ";");

while (st.hasMoreTokens()){

String s = st.nextToken().trim();

v.add(s);

}

return v;

}



/**

* Carga los concursos disponibles en la lista de concursos.

* @param concursos Vector de Concursos con los concursos disponibles.

*/

private void cargarConcursos(Vector concursos) {

concursosLista.clear();

this.concursos = concursos;

for (int i=0;i<concursos.size();i++){

Concursos c = (Concursos)concursos.elementAt(i);

concursosLista.addElement(c.getAcronimo() + " - " + c.getDescripcion());

}

listaConcursos.setSelectedIndex(0);

}



/**

* Carga las restriciones de un concurso que recibe como parámetro

* en la lista de restricciones.

* @param concurso Concurso del que se obtienen las restricciones a visualizar.

*/

private void cargarRestriciones(Concursos c){

restricLista.clear();

List restricciones = c.getRestricciones();

for (Iterator it= restricciones.iterator();it.hasNext();)

{

Restricciones restriccion = (Restricciones) it.next();

restricLista.addElement("Opción: "+ restriccion.getOpcion());

}

}



/**

* Limpia los campos para dar de alta un concurso o una restricción.

*/

private void limpiarDatosConcursos(){

opciones.setText("");

palabrasNoPermitidas.setText("");

descripcion.setText("");

acronimo.setText("");

}
public static void main(String[] args)

{

VentanaServidorConcursos ventana = new VentanaServidorConcursos();

ventana.show();

}
private javax.swing.JTextField acronimo;
private javax.swing.JTextField descripcion;
private javax.swing.JButton botonNoPermitidas;
private javax.swing.JButton botonOpciones;
private javax.swing.JButton botonNuevoC;
private javax.swing.JButton botonSalir;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel titulo;
private javax.swing.JTextField palabrasNoPermitidas;
private javax.swing.JTextField opciones;
private javax.swing.JList listaConcursos;
private javax.swing.JList listaRestric;
private javax.swing.JPanel panelTitulo;
private javax.swing.JPanel panelBotonesC;
private javax.swing.JPanel panelBoton;
private javax.swing.JPanel panelConcursos;
private javax.swing.JPanel panelControlC;
private javax.swing.JPanel panelRMalsonantes;
private javax.swing.JPanel panelROpciones;
private javax.swing.JPanel panelListaC;
private javax.swing.JPanel panelListaR;
private javax.swing.JPanel panelCentral;
private javax.swing.JScrollPane scrollConcursos;
private javax.swing.JScrollPane scrollRestric;
private javax.swing.JTabbedPane tablaPestañas;
private javax.swing.DefaultListModel concursosLista;
private javax.swing.DefaultListModel restricLista;



And the code for building the sessionFactory in GestorConcursos class is this:


public GestorConcursos() throws Exception

{

try

{

sf= new Configuration()

.addClass(Concursos.class)

.addClass(Restricciones.class)

.buildSessionFactory();

session = sf.openSession();

}

catch (Exception e)

{

throw e;

}

}
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 08, 2005 6:34 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
So what is line 44 in this mess? "at org.hibernate.entregar.gui.VentanaServidorConcursos.<init>(Ve
ntanaServidorConcursos.java:44)"...

btw, this forum does have code tags so that it makes it easier for everyone to read code that you post here, rather than this pile of left-justified dodo ;)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 08, 2005 6:42 am 
Regular
Regular

Joined: Thu Dec 02, 2004 7:11 am
Posts: 85
Side note - when post code, use "code" tag.

Looks like GestorConcursos constructor is throwing some exception, but in method crearGC() you are silently swallow it.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 08, 2005 3:09 pm 
Newbie

Joined: Sat Aug 06, 2005 10:25 am
Posts: 3
You are right with the way of posting code, sorry for that. I have solved this, it was a fooly mistake, I had not declared a no-argument constructor for class Restricciones and I had forgotten to declare a set method for an attribute. Now it does not crash but the queries don't work. If I can't fix this I will ask for help again.

Thank you for the help!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.