javax.faces.el.EvaluationException: java.lang.NullPointerException
Bonsoir,
Pourriez vous s'il vous plait m'aider à résoudre le problème suivant:
classe "AuthorCtrl" du package control
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
package control;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import model.dao.AuthorDao;
import model.dto.Author;
public class AuthorCtrl {
private AuthorDao Daoauteur = new AuthorDao();
private DataModel auteur;
private Author editAuthor;
private Author newAuthor = new Author();
public Author getEditAuthor() {
return editAuthor;
}
public void setEditAuthor(Author editAuthor) {
this.editAuthor = editAuthor;
}
public Author getNewAuthor() {
return newAuthor;
}
public void setNewAuthor(Author newAuthor) {
this.newAuthor = newAuthor;
}
public DataModel getAuthor() {
if (auteur == null) {
auteur = new ListDataModel();
}
return auteur;
}
public String createAuthor() {
Daoauteur.insert(newAuthor);
newAuthor = new Author();
auteur.setWrappedData(Daoauteur.selectAll());
return "listauteur";
}
public String deleteAuthor() {
Author p = (Author) auteur.getRowData();
Daoauteur.delete(p);
auteur.setWrappedData(Daoauteur.selectAll());
return null;
}
public String editAuthor() {
editAuthor = (Author) auteur.getRowData();
return "editauteur";
}
public String updateAuthor() {
Daoauteur.update(editAuthor);
auteur.setWrappedData(Daoauteur.selectAll());
return "updateauteur";
}
} |
classe "ProjectCtrl" du package control
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
package control;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import model.dao.ProjectDao;
import model.dto.Project;
public class ProjectCtrl {
private ProjectDao pDao = new ProjectDao();
private DataModel projets;
private Project editProject;
private Project newProject = new Project();
public Project getEditProject() {
return editProject;
}
public void setEditProject(Project editProject) {
this.editProject = editProject;
}
public Project getNewProject() {
return newProject;
}
public void setNewProject(Project newProject) {
this.newProject = newProject;
}
public DataModel getProject() {
if (projets == null) {
projets = new ListDataModel();
projets.setWrappedData(pDao.selectAll());
}
return projets;
}
public String createProject() {
pDao.insert(newProject);
newProject = new Project();
projets.setWrappedData(pDao.selectAll());
return "list";
}
public String deleteProject() {
Project p = (Project) projets.getRowData();
pDao.delete(p);
projets.setWrappedData(pDao.selectAll());
return null;
}
public String editProject() {
editProject = (Project) projets.getRowData();
return "edit";
}
public String updateProject() {
pDao.update(editProject);
projets.setWrappedData(pDao.selectAll());
return "update";
}
} |
classe "AuthorDao" du package model.dao
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| package model.dao;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import model.dto.Author;
/**
* C'est le DAO qui permet d'effectuer des opérations portant sur un auteur
* dans la base de données.
*
*/
public class AuthorDao {
private static final String JPA_UNIT_NAME = "project";
private EntityManager entityManager;
protected EntityManager getEntityManager() {
if (entityManager == null) {
entityManager = Persistence.createEntityManagerFactory(JPA_UNIT_NAME).createEntityManager();
}
return entityManager;
}
/**
* @return toutes les auteurs dans la base de données.
*/
public List<Author> selectAll() {
List<Author> auteur = getEntityManager().createQuery("select p from Author p").getResultList();
return auteur;
}
/**
* @param a L'auteur à insérer dans la base de données.
* @return L'auteur inséré.
*/
public Author insert(Author a) {
getEntityManager().getTransaction().begin();
getEntityManager().persist(a);
getEntityManager().getTransaction().commit();
return a;
}
/**
* @param a L'auteur à supprimer de la base de donnés.
*/
public void delete(Author a) {
getEntityManager().getTransaction().begin();
a = getEntityManager().merge(a);
getEntityManager().remove(a);
getEntityManager().getTransaction().commit();
}
/**
* @param a L'auteur à mettre à jour dans la base de données.
* @return L'auteur mis à jour
*/
public Author update(Author a) {
getEntityManager().getTransaction().begin();
a = getEntityManager().merge(a);
getEntityManager().getTransaction().commit();
return a;
}
} |
classe "ProjectDao" du package model.dao
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
package model.dao;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import model.dto.Project;
public class ProjectDao {
private static final String JPA_UNIT_NAME = "project";
private EntityManager entityManager;
protected EntityManager getEntityManager() {
if (entityManager == null) {
entityManager = Persistence.createEntityManagerFactory(JPA_UNIT_NAME).createEntityManager();
}
return entityManager;
}
public List<Project> selectAll() {
List<Project> projets = getEntityManager().createQuery("select p from Project p").getResultList();
return projets;
}
public Project insert(Project u) {
getEntityManager().getTransaction().begin();
getEntityManager().persist(u);
getEntityManager().getTransaction().commit();
return u;
}
public void delete(Project u) {
getEntityManager().getTransaction().begin();
u = getEntityManager().merge(u);
getEntityManager().remove(u);
getEntityManager().getTransaction().commit();
}
public Project update(Project u) {
getEntityManager().getTransaction().begin();
u = getEntityManager().merge(u);
getEntityManager().getTransaction().commit();
return u;
}
} |
classe "Author" du package model.dto
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
package model.dto;
import javax.persistence.*;
import static javax.persistence.GenerationType.IDENTITY;
import static javax.persistence.DiscriminatorType.INTEGER;
@Entity
@Table(name="author", schema = "baseprojet")
public class Author {
private String firstname;
private String familyname;
@Id
@GeneratedValue(strategy=IDENTITY)
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstname;
}
public void setFirstName(String firstname) {
this.firstname = firstname;
}
public String getFamilyName() {
return familyname;
}
public void setFamilyName(String familyname) {
this.familyname = familyname;
}
public String toString(){
return familyname +" "+ firstname;
}
} |
classe "Project" du package model.dto
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
package model.dto;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import static javax.persistence.GenerationType.IDENTITY;
@Entity
@Table(name="project", schema="baseprojet")
public class Project {
private Long id;
private int auteur_id;
private String nomprojet;
@Id
@GeneratedValue(strategy=IDENTITY)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public int getAuteur_id() {
return auteur_id;
}
public void setAuteur_id(int auteur_id) {
this.auteur_id = auteur_id;
}
public String getNomprojet() {
return nomprojet;
}
public void setNomprojet(String nomprojet) {
this.nomprojet = nomprojet;
}
} |
fichier persistence.xml
Code:
1 2 3 4 5 6 7 8 9
|
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="project" transaction-type="JTA">
<jta-data-source>jdbc/_baseprojet</jta-data-source>
<class>model.dto.Project</class>
<class>model.dto.Author</class>
</persistence-unit>
</persistence> |
fichier faces config.xml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
version="1.2">
<managed-bean>
<managed-bean-name>projectCtrl</managed-bean-name>
<managed-bean-class>control.ProjectCtrl</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>authorCtrl</managed-bean-name>
<managed-bean-class>control.AuthorCtrl</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
<display-name>add</display-name>
<from-view-id>/add.jsp</from-view-id>
<navigation-case>
<from-outcome>list</from-outcome>
<to-view-id>/list.jsp</to-view-id>
<redirect />
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/list.jsp</from-view-id>
<navigation-case>
<from-outcome>edit</from-outcome>
<to-view-id>/edit.jsp</to-view-id>
<redirect />
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/edit.jsp</from-view-id>
<navigation-case>
<from-outcome>update</from-outcome>
<to-view-id>/list.jsp</to-view-id>
<redirect />
</navigation-case>
</navigation-rule>
</faces-config> |
code source de la page jsp "addauteur.jsp"
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<f:view>
<h:form>
<h:panelGrid border="0" columns="3" cellpadding="5">
<h:outputText value="Prénom de l'auteur" />
<h:inputText id="name" value="#{authorCtrl.newAuthor.firstName}" required="true" requiredMessage="Nom obligatoire" />
<h:message for="name" />
<h:outputText value="Nom de l'auteur" />
<h:inputText id="nomFamille" value="#{authorCtrl.newAuthor.familyName}" required="true" requiredMessage="Nom obligatoire" />
<h:message for="nomFamille" />
<h:outputText />
<h:commandButton value="Ajouter" action="#{authorCtrl.createAuthor}" />
</h:panelGrid>
<h:panelGrid columns="2" cellpadding="10">
<h:outputLink value="list.jsf">
<h:outputText value="Lister" />
</h:outputLink>
</h:panelGrid>
</h:form>
</f:view> |
Le problème est le suivant: lorsque je veux ajouter un auteur, la page jsp se lance très bien je peux renseigner les deux champs. En vérifiant dans ma base de donnée, le champ a bien été ajouté mais sur ma page internet j'obtiens l'erreur suivante:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| type Exception report
message
descriptionThe server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: #{authorCtrl.createAuthor}: java.lang.NullPointerException
root cause
javax.faces.FacesException: #{authorCtrl.createAuthor}: java.lang.NullPointerException
root cause
javax.faces.el.EvaluationException: java.lang.NullPointerException
root cause
java.lang.NullPointerException |
En enlevant la ligne
Code:
1 2
|
auteur.setWrappedData(Daoauteur.selectAll()); |
de la classe "AuthorCtrl" je n'ai plus d'erreur mais ça ne m'avance pas plus...
Auriez vous une idée d'où peut provenir cette erreur?
Je vous remercie par avance pour votre aide,
Damien