Exception in thread "main" java.lang.NullPointerException
je suis debutant en JPA je developpe avec netbeans avec un serveur integrer glassfish v 2.1 je vient de faire un test mais un erreur se declenche voila l'erreur :
------------------------------------------------
init:
deps-module-jar:
deps-ear-jar:
deps-jar:
compile-single:
run-main:
Exception in thread "main" java.lang.NullPointerException
at ma.metier.test.main(test.java:15)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
---------------------------------------------------
voila la classe de test :
package ma.metier;
import javax.persistence.*;
import javax.persistence.PersistenceContext;
import javax.persistence.Persistence;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceUnit;
import rmtDAObase.Formateurs;
public class test {
@PersistenceUnit(unitName="monUnite")
public static EntityManagerFactory emf;
public static void main(String[]argv) {
// EntityManagerFactory emf = Persistence.createEntityManagerFactory("monUnite");
EntityManager em = emf.createEntityManager();
EntityTransaction trans = em.getTransaction();
trans.begin();
Formateurs f =em.find(Formateurs.class, 1);
if(f!=null){
System.out.println("formateur.nom="+f.getNom());
}
trans.commit();
em.close();
emf.close();
}
}
---------------------------------------------------------
voila mon entity :
package rmtDAObase;
/***********************************************************************
* Module: Formateurs.java
* Author: hiddouche
* Purpose: Defines the Class Formateurs
***********************************************************************/
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import static javax.persistence.GenerationType.IDENTITY;
@Entity
@Table(name = "formateurs")
//@NamedQueries( {@NamedQuery(name = "formateurs.findByMatricule", query = "SELECT w FROM formateurs w WHERE w.matricule = :matricule")})
// , @NamedQuery(name = "formateurs.findByNom", query = "SELECT w FROM formateurs w WHERE w.nom = :nom"), @NamedQuery(name = "formateurs.findByPrenom", query = "SELECT w FROM formateurs w WHERE w.prenom = :prenom"), @NamedQuery(name = "formateurs.findByAdresse", query = "SELECT w FROM formateurs w WHERE w.adresse = :adresse"), @NamedQuery(name = "formateurs.findByTel", query = "SELECT w FROM formateurs w WHERE w.tel = :tel")
public class Formateurs implements Serializable{
@Id
@GeneratedValue(strategy=IDENTITY)
@Column(name = "matricule", nullable = false)
private Integer matricule;
@Column(name = "nom", nullable = false)
private java.lang.String nom;
@Column(name = "prenom", nullable = false)
private java.lang.String prenom;
@Column(name = "adresse", nullable = false)
private java.lang.String adresse;
@Column(name = "tel", nullable = false)
private java.lang.String tel;
@Transient
private java.util.Collection seance;
public Formateurs() {
}
public java.util.Collection getSeance() {
if (seance == null)
seance = new java.util.HashSet();
return seance;
}
public java.util.Iterator getIteratorSeance() {
if (seance == null)
seance = new java.util.HashSet();
return seance.iterator();
}
public void setSeance(java.util.Collection newSeance) {
removeAllSeance();
for (java.util.Iterator iter = newSeance.iterator(); iter.hasNext();)
addSeance((Seance)iter.next());
}
public void addSeance(Seance newSeance) {
if (newSeance == null)
return;
if (this.seance == null)
this.seance = new java.util.HashSet();
if (!this.seance.contains(newSeance))
{
this.seance.add(newSeance);
newSeance.setFormateurs(this);
}
}
public void removeSeance(Seance oldSeance) {
if (oldSeance == null)
return;
if (this.seance != null)
if (this.seance.contains(oldSeance))
{
this.seance.remove(oldSeance);
oldSeance.setFormateurs((Formateurs)null);
}
}
public String getAdresse() {
return adresse;
}
public void setAdresse(String adresse) {
this.adresse = adresse;
}
public Integer getMatricule() {
return matricule;
}
public void setMatricule(Integer matricule) {
this.matricule = matricule;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getPrenom() {
return prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public void removeAllSeance() {
if (seance != null)
{
Seance oldSeance;
for (java.util.Iterator iter = getIteratorSeance(); iter.hasNext();)
{
oldSeance = (Seance)iter.next();
iter.remove();
oldSeance.setFormateurs((Formateurs)null);
}
}
}
// a ajouter et a chercher
public int hashCode() {
int hash = 0;
hash += (this.matricule != null ? this.matricule.hashCode() : 0);
return hash;
}
public boolean equals(Object object) {
if (object == null || !this.getClass().equals(object.getClass())) {
return false;
}
Formateurs other = (Formateurs)object;
if (this.matricule != other.matricule && (this.matricule == null || !this.matricule.equals(other.matricule))) return false;
return true;
}
public String toString() {
//TODO change toString() implementation to return a better display name
return "" + this.matricule;
}
}
-------------------------------------------------------
voila le fichier persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="monUnite" transaction-type="JTA">
<jta-data-source>jndiGET</jta-data-source>
<class>rmtDAObase.Formateurs</class>
<class>enterprise.jsf_jpa_war.Wuser</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/get"/>
<property name="toplink.jdbc.user" value="root"/>
<property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="toplink.jdbc.password" value=""/>
</properties>
</persistence-unit>
</persistence>
----------------------------------------------
est ce quelq'un peut m'aider ?
et merci
entityManagerFactory NULL
Citation:
Envoyé par
hiddouche
Exception in thread "main" java.lang.NullPointerException
at ma.metier.test.main(test.java:15)
Ce pointeur NULL est détecté dans la ligne 15
Citation:
// EntityManagerFactory emf = Persistence.createEntityManagerFactory("monUnite");
EntityManager em = emf.createEntityManager();
et c'est ça l'erreur: donc tu as commenté l'instruction qui sert à fabriquer the ENTITY MANAGER FACTORY et qui est responsable de créer the ENTITY manager donc l'instance emf est null.
donc tu doit décommenter cette ligne.
bon chance.