IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Développement Web en Java Discussion :

Exception de type java.lang.NumberFormatException


Sujet :

Développement Web en Java

  1. #1
    Membre averti
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2014
    Messages
    29
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Bénin

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2014
    Messages : 29
    Par défaut Exception de type java.lang.NumberFormatException
    Bonjour, je suis nouveau en Java EE.

    Je m’exerce sur une application web Java EE de gestion de stock à travers les différentes requête, d'insertion, de modification, de suppression, et de sélection.

    Mais voila lorsque je traite la requête de modification dans ma servlet je rencontre un Exception du type : java.lang.NumberFormatException: null.

    Ci-dessous vous verrai une copie du code de mes différentes classes et de ma jsp.

    Bean Magasin :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    package com.bean;
     
    public class Magasin {
     
        private Long idMagasin;
        private String nomMagasin, adresseMagasin, telephoneMagasin;
     
        public Long getIdMagasin() {
            return idMagasin;
        }
     
        public void setIdMagasin(Long idMagasin) {
            this.idMagasin = idMagasin;
        }
     
        public String getNomMagasin() {
            return nomMagasin;
        }
     
        public void setNomMagasin(String nomMagasin) {
            this.nomMagasin = nomMagasin;
        }
     
        public String getAdresseMagasin() {
            return adresseMagasin;
        }
     
        public void setAdresseMagasin(String adresseMagasin) {
            this.adresseMagasin = adresseMagasin;
        }
     
        public String getTelephoneMagasin() {
            return telephoneMagasin;
        }
     
        public void setTelephoneMagasin(String telephoneMagasin) {
            this.telephoneMagasin = telephoneMagasin;
        }
     
        @Override
        public String toString() {
            return "Magasin [idMagasin=" + idMagasin + ", nomMagasin=" + nomMagasin
                    + ", adresseMagasin=" + adresseMagasin + ", telephoneMagasin="
                    + telephoneMagasin + "]";
        }
    }
    DaoFactory :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
     
    package com.dao;
     
    import java.io.InputStream;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.Properties;
     
    public class DaoFactory {
     
        private static final String FICHIER_PROPERTY = "/com/dao/dao.properties";
        private static final String PROPERTY_DRIVER = "driver";
        private static final String PROPERTY_URL = "url";
        private static final String PROPERTY_USERNAME = "username";
        private static final String PROPERTY_PASSWORD = "password";
     
        private String url;
        private String username;
        private String password;
     
        public DaoFactory(String url, String username, String password){
            this.url = url;
            this.username = username;
            this.password = password;
        }
     
        public static DaoFactory getInstance() throws DaoConfigurationException{
            Properties properties = new Properties();
            String driver;
            String url;
            String username;
            String password;
     
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            InputStream fichierProperties = classLoader.getResourceAsStream(FICHIER_PROPERTY);
     
            if(fichierProperties == null){
                throw new DaoConfigurationException("Le fichier contenant les paramètres de connexion "+FICHIER_PROPERTY+" est introuvable.");
            }
     
            try{
                properties.load(fichierProperties);
                driver = properties.getProperty(PROPERTY_DRIVER);
                url = properties.getProperty(PROPERTY_URL);
                username = properties.getProperty(PROPERTY_USERNAME);
                password = properties.getProperty(PROPERTY_PASSWORD);
            }catch(IOException e){
                throw new DaoConfigurationException("Echec de chargement des paramètres de connexion depuis le fichier "+FICHIER_PROPERTY, e.getCause());
            }
     
            try{
                Class.forName(driver);
            }catch(ClassNotFoundException e){
                throw new DaoConfigurationException("Echec de chargement des pilotes.", e.getCause());
            }
     
            DaoFactory instance = new DaoFactory(url, username, password);
     
            return instance;
        }
     
        Connection getConnection() throws SQLException{
            return DriverManager.getConnection(url, username, password);
        }
     
        public ProduitDao getProduitDao(){
            return new ProduitDaoImpl(this);
        }
     
        public FamilleProduitDao getFamilleProduitDao(){
            return new FamilleProduitDaoImpl(this);
        }
     
        public MagasinDao getMagasinDao(){
            return new MagasinDaoImpl(this);
        }
    }
    Interface MagasinDao :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
     
    package com.dao;
     
    import java.util.List;
     
    import com.bean.Magasin;
     
    public interface MagasinDao {
     
        void creerMagasin(Magasin magasin) throws DaoException;
     
        List<Magasin> selectionnerMagasin() throws DaoException;
     
        Magasin selectionnerMagasinParId(Long idMagasin) throws DaoException;
     
        void modifierMagasin(Magasin magasin) throws DaoException;
     
        void supprimerMagasin(Magasin magasin) throws DaoException;
    }
    MagasinDaoImpl (Classe implemantant l'interface MagasinDao) :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
     
    package com.dao;
     
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
     
    import com.bean.Magasin;
     
    import static com.dao.DaoUtilitaires.*;
     
    public class MagasinDaoImpl implements MagasinDao {
     
        private static final String SQL_DELETE_MAGASIN = "DELETE FROM magasin WHERE id_magasin = ?";
        private static final String SQL_INSERT_MAGASIN = "INSERT INTO magasin(nom_magasin, adresse_magasin, telephone_magasin) VALUES(?, ?, ?)";
        private static final String SQL_SELECT_MAGASIN = "SELECT * FROM magasin";
        private static final String SQL_SELECT_MAGASIN_PAR_ID = "SELECT * FROM magasin WHERE id_magasin = ?";
        private static final String SQL_UPDATE_MAGASIN = "UPDATE magasin SET nom_magasin = ?, adresse_magasin = ?, telephone_magasin = ? WHERE id_magasin = ?";
     
        private DaoFactory daoFactory;
     
        public MagasinDaoImpl(DaoFactory daoFactory){
            this.daoFactory = daoFactory;
        }
     
        public Magasin map(ResultSet resultSet) throws SQLException{
            Magasin magasin = new Magasin();
     
            magasin.setIdMagasin(resultSet.getLong("id_magasin"));
            magasin.setNomMagasin(resultSet.getString("nom_magasin"));
            magasin.setAdresseMagasin(resultSet.getString("adresse_magasin"));
            magasin.setTelephoneMagasin(resultSet.getString("telephone_magasin"));
     
            return magasin;
        }
     
        @Override
        public void creerMagasin(Magasin magasin) throws DaoException {
            Connection connection = null;
            PreparedStatement preparedStatement = null;
            ResultSet valeurAutoGeneree = null;
     
            try{
                connection = daoFactory.getConnection();
                preparedStatement = initialisationRequetePreparee(connection, SQL_INSERT_MAGASIN, true, magasin.getNomMagasin(), magasin.getAdresseMagasin(), magasin.getTelephoneMagasin());
                int statut = preparedStatement.executeUpdate();
     
                if(statut == 0){
                    throw new DaoException("Echec d'insertion d'enregistrement dans la base.");
                }
     
                valeurAutoGeneree = preparedStatement.getGeneratedKeys();
     
                if(valeurAutoGeneree.next()){
                    magasin.setIdMagasin(valeurAutoGeneree.getLong(1));
                }else{
                    throw new DaoException("Echec de creation de valeur auto générée.");
                }
     
            }catch(SQLException e){
                throw new DaoException(e);
            }finally{
                fermetureSilencieuse(valeurAutoGeneree, preparedStatement, connection);
            }
        }
     
        @Override
        public List<Magasin> selectionnerMagasin() throws DaoException {
            Connection connection = null;
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            List<Magasin> listeMagasin = new ArrayList<Magasin>();
     
            try{
                connection = daoFactory.getConnection();
                preparedStatement = initialisationRequetePreparee(connection, SQL_SELECT_MAGASIN, false);
                resultSet = preparedStatement.executeQuery();
     
                while(resultSet.next()){
                    listeMagasin.add(map(resultSet));
                }
     
            }catch(SQLException e){
                throw new DaoException(e);
            }finally{
                fermetureSilencieuse(resultSet, preparedStatement, connection);
            }
     
            return listeMagasin;
        }
     
        @Override
        public Magasin selectionnerMagasinParId(Long idMagasin) throws DaoException {
            Connection connection = null;
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            Magasin magasin = null;
     
            try{
                connection = daoFactory.getConnection();
                preparedStatement = initialisationRequetePreparee(connection, SQL_SELECT_MAGASIN_PAR_ID, false, idMagasin);
                resultSet = preparedStatement.executeQuery();
     
                if(resultSet.next()){
                    magasin = map(resultSet);
                }
     
            }catch(SQLException e){
                throw new DaoException(e);
            }finally{
                fermetureSilencieuse(resultSet, preparedStatement, connection);
            }
     
            return magasin;
        }
     
        @Override
        public void modifierMagasin(Magasin magasin) throws DaoException {
            Connection connection = null;
            PreparedStatement preparedStatement = null;
     
            try{
                connection = daoFactory.getConnection();
                preparedStatement = initialisationRequetePreparee(connection, SQL_UPDATE_MAGASIN, true, magasin.getNomMagasin(), magasin.getAdresseMagasin(), magasin.getTelephoneMagasin(), magasin.getIdMagasin());
                int statut = preparedStatement.executeUpdate();
     
                if(statut == 0){
                    throw new DaoException("Echec de modification de l'enregistrement.");
                }
     
            }catch(SQLException e){
                throw new DaoException(e);
            }finally{
                fermetureSilencieuse(preparedStatement, connection);
            }
     
        }
     
        @Override
        public void supprimerMagasin(Magasin magasin) throws DaoException {
            Connection connection = null;
            PreparedStatement preparedStatement = null;
     
            try{
                connection = daoFactory.getConnection();
                preparedStatement = initialisationRequetePreparee(connection, SQL_DELETE_MAGASIN, true, magasin.getIdMagasin());
                int statut = preparedStatement.executeUpdate();
     
                if(statut == 0){
                    throw new DaoException("Echec de suppression de l'enregistrement.");
                }
     
            }catch(SQLException e){
                throw new DaoException(e);
            }finally{
                fermetureSilencieuse(preparedStatement, connection);
            }
        }
    }
    Classe ModifierMagasinForm :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    package com.servlet;
     
    import java.io.IOException;
     
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.ServletException;
     
    import com.bean.Magasin;
    import com.dao.DaoFactory;
    import com.dao.MagasinDao;
    import com.form.ModifierMagasinForm;
     
    public class ModifierMagasin extends HttpServlet {
     
        private static final String ATT_CONF_DAO_FACTORY = "daofactory";
        private static final String ATT_FORM = "form";
        private static final String ATT_MAGASIN = "magasin";
        private static final String PARAM_ID_MAGASIN = "idMagasin";
        private static final String VUE_MODIFIER_MAGASIN = "/WEB-INF/modifiermagasin.jsp";
     
        private MagasinDao magasinDao;
     
        public void init() throws ServletException{
            this.magasinDao = ((DaoFactory)getServletContext().getAttribute(ATT_CONF_DAO_FACTORY)).getMagasinDao();
        }
     
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
     
            String valeurParametre = getValeurParametre(request, PARAM_ID_MAGASIN);
     
            Magasin magasin = magasinDao.selectionnerMagasinParId(Long.parseLong(valeurParametre));
     
            request.setAttribute(ATT_MAGASIN, magasin);
     
            this.getServletContext().getRequestDispatcher(VUE_MODIFIER_MAGASIN).forward(request, response);
        }
     
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
     
            ModifierMagasinForm form = new ModifierMagasinForm(magasinDao);
            Magasin magasin = form.modifierMagasin(request);
     
            request.setAttribute(ATT_FORM, form);
            request.setAttribute(ATT_MAGASIN, magasin);
     
            this.getServletContext().getRequestDispatcher(VUE_MODIFIER_MAGASIN).forward(request, response);
        }
     
        private String getValeurParametre(HttpServletRequest request, String nomParametre){
            String valeurParametre = request.getParameter(nomParametre);
     
            if(valeurParametre == null || valeurParametre.trim().length() == 0){
                return null;
            }else{
                return valeurParametre.trim();
            }
        }
    }
    JSP listemagasin :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
     
    <%@ page pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title>Liste des magasin</title>
            <link rel="stylesheet" type="text/css" href="<c:url value="/inc/style.css"/>"/>
        </head>
        <body>
            <div id="main">
                <%-- Entete de page --%>
                <div id="header">
                  <div id="banner">
                    <div id="welcome"><h1>Online Stock Application</h1></div>
                    <div id="welcome_slogan"><h1>Always think big</h1></div>
                  </div>
                </div>
     
                <%-- Barre de menu --%>
                <div id="menubar">
                    <c:import url="/inc/menu.jsp"/>
                </div>
     
                <%-- Contenu de la page --%>
                <div id="site_content">
                    <c:choose>
                        <c:when test="${empty listemagasin}"><c:out value="Pas de produit à afficher"/></c:when>
                        <c:otherwise>
                            <table border="1">
                                <tr>
                                   <th style="width: 80px;">Code magasin</th>
                                   <th style="width: 200px;">Nom</th>
                                   <th style="width: 200px;">Adresse</th>
                                   <th style="width: 180px;">Téléphone</th>
                                   <th style="width: 100px;">Modifier</th>
                                   <th style="width: 100px;">Supprimer</th>
                                </tr>
                                    <c:forEach items="${listemagasin}" var="varListeMagasin" varStatus="double">
                                        <tr>
                                            <td>${varListeMagasin.idMagasin}</td>
                                            <td>${varListeMagasin.nomMagasin}</td>
                                            <td>${varListeMagasin.adresseMagasin}</td>
                                            <td>${varListeMagasin.telephoneMagasin}</td>
                                            <td><a href="<c:url value="modifiermagasin"><c:param name="idMagasin" value="${varListeMagasin.idMagasin}"/></c:url>">Modifier</a></td>
                                            <td><a href="<c:url value="supprimermagasin"><c:param name="idMagasin" value="${varListeMagasin.idMagasin}"/></c:url>">Supprimer</a></td>                                    
                                        </tr>                                                                    
                                    </c:forEach>
                            </table>
                        </c:otherwise>
                    </c:choose>
                    <p style="margin: 10px 20px 0 0; float: right;"> | <a href="connection.jsp">Logout</a></p>
                </div>
     
                <%--Info pied de page --%>
                <div id="content_grey">
                    <div class="content_grey_container_box">
                        <h4>Latest Blog Post</h4>
                        <p> Phasellus laoreet feugiat risus. Ut tincidunt, ante vel fermentum iaculis.</p>
                        <div class="readmore"><a href="#">Read more</a></div>
                    </div>
     
                    <div class="content_grey_container_box">
                        <h4>Latest News</h4>
                        <p> Phasellus laoreet feugiat risus. Ut tincidunt, ante vel fermentum iaculis.</p>
                        <div class="readmore"><a href="#">Read more</a></div>
                    </div>
     
                    <div class="content_grey_container_boxl">
                        <h4>Latest Projects</h4>
                        <p> Phasellus laoreet feugiat risus. Ut tincidunt, ante vel fermentum iaculis.</p>
                        <div class="readmore"><a href="#">Read more</a></div>
                    </div>
     
                    <br style="clear:both"/>
     
                </div>
     
            </div>
     
            <%-- Pied de page --%>
            <div id="footer">
                <a href="#">Valid XHTML</a> | <a href="#">Valid CSS</a> | <a href="#">Images</a> | Conception <a href="#">Alco Services</a>
            </div>
        </body>
    </html>
    JSP modifiermagasin :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
     
    <%@ page pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title>Modifier un magasin</title>
            <link rel="stylesheet" type="text/css" href="<c:url value="/inc/style.css"/>"/>
        </head>
        <body>
            <div id="main">
                <%-- Entete de page --%>
                <div id="header">
                  <div id="banner">
                    <div id="welcome"><h1>Online Stock Application</h1></div>
                    <div id="welcome_slogan"><h1>Always think big</h1></div>
                  </div>
                </div>
     
                <%-- Barre de menu --%>
                <div id="menubar">
                    <c:import url="/inc/menu.jsp"/>
                </div>
     
                <%-- Contenu de la page --%>
                <div id="site_content">
                    <form action="modifiermagasin" method="post">
                    <label for="idMagasin">Code magasin : <span>*</span></label>
                        <input type="text" id="idMagasin" name="idMagasin" value="<c:out value="${magasin.idMagasin}"/>" size="50" maxlength="250" disabled/><br/>
                        <label for="nomMagasin">Nom magasin <span>*</span></label>
                        <input type="text" id="nomMagasin" name="nomMagasin" value="<c:out value="${magasin.nomMagasin}"/>" size="50" maxlength="250"/><span>${form.erreur['nomMagasin']}</span><br/>
                        <label for="adresseMagasin">Adresse <span>*</span></label>
                        <input type="text" id="adresseMagasin" name="adresseMagasin" value="<c:out value="${magasin.adresseMagasin}"/>" size="50" maxlength="250"/><span>${form.erreur['adresseMagasin']}</span><br/>
                        <label for="telephoneMagasin">Telephone <span>*</span></label>
                        <input type="text" id="telephoneMagasin" name="telephoneMagasin" value="<c:out value="${magasin.telephoneMagasin}"/>" size="50" maxlength="250"/><span>${form.erreur['telephoneMagasin']}</span><br/>
                        <input type="submit" id="btnEnvoyer" name="btnEnvoyer" value="Valider"/>
                        <input type="reset" id="btnAnnuler" name="btnAnnuler" value="Annuler"/>
                        <p><c:if test="${!empty form.erreur}"><c:out value="${form.resultat}"/></c:if></p>
                    </form>
                </div>
     
                <%--Info pied de page --%>
                <div id="content_grey">
                    <div class="content_grey_container_box">
                        <h4>Latest Blog Post</h4>
                        <p> Phasellus laoreet feugiat risus. Ut tincidunt, ante vel fermentum iaculis.</p>
                        <div class="readmore"><a href="#">Read more</a></div>
                    </div>
     
                    <div class="content_grey_container_box">
                        <h4>Latest News</h4>
                        <p> Phasellus laoreet feugiat risus. Ut tincidunt, ante vel fermentum iaculis.</p>
                        <div class="readmore"><a href="#">Read more</a></div>
                    </div>
     
                    <div class="content_grey_container_boxl">
                        <h4>Latest Projects</h4>
                        <p> Phasellus laoreet feugiat risus. Ut tincidunt, ante vel fermentum iaculis.</p>
                        <div class="readmore"><a href="#">Read more</a></div>
                    </div>
     
                    <br style="clear:both"/>
     
                </div>
     
            </div>
     
            <%-- Pied de page --%>
            <div id="footer">
                <a href="#">Valid XHTML</a> | <a href="#">Valid CSS</a> | <a href="#">Images</a> | Conception <a href="#">Alco Services</a>
            </div>
        </body>
    </html>
    Servlet ModifierMagasin :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    package com.servlet;
     
    import java.io.IOException;
     
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.ServletException;
     
    import com.bean.Magasin;
    import com.dao.DaoFactory;
    import com.dao.MagasinDao;
    import com.form.ModifierMagasinForm;
     
    public class ModifierMagasin extends HttpServlet {
     
        private static final String ATT_CONF_DAO_FACTORY = "daofactory";
        private static final String ATT_FORM = "form";
        private static final String ATT_MAGASIN = "magasin";
        private static final String PARAM_ID_MAGASIN = "idMagasin";
        private static final String VUE_MODIFIER_MAGASIN = "/WEB-INF/modifiermagasin.jsp";
     
        private MagasinDao magasinDao;
     
        public void init() throws ServletException{
            this.magasinDao = ((DaoFactory)getServletContext().getAttribute(ATT_CONF_DAO_FACTORY)).getMagasinDao();
        }
     
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
     
            String valeurParametre = getValeurParametre(request, PARAM_ID_MAGASIN);
     
            Magasin magasin = magasinDao.selectionnerMagasinParId(Long.parseLong(valeurParametre));
     
            request.setAttribute(ATT_MAGASIN, magasin);
     
            this.getServletContext().getRequestDispatcher(VUE_MODIFIER_MAGASIN).forward(request, response);
        }
     
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
     
            ModifierMagasinForm form = new ModifierMagasinForm(magasinDao);
            Magasin magasin = form.modifierMagasin(request);
     
            request.setAttribute(ATT_FORM, form);
            request.setAttribute(ATT_MAGASIN, magasin);
     
            this.getServletContext().getRequestDispatcher(VUE_MODIFIER_MAGASIN).forward(request, response);
        }
     
        private String getValeurParametre(HttpServletRequest request, String nomParametre){
            String valeurParametre = request.getParameter(nomParametre);
     
            if(valeurParametre == null || valeurParametre.trim().length() == 0){
                return null;
            }else{
                return valeurParametre.trim();
            }
        }
    }
    Message d'erreur :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
     
    mai 11, 2017 10:35:06 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
    AVERTISSEMENT: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:gestionclient' did not find a matching property.
    mai 11, 2017 10:35:06 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
    AVERTISSEMENT: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:gestioncommande' did not find a matching property.
    mai 11, 2017 10:35:06 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
    AVERTISSEMENT: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:gestionconnexion' did not find a matching property.
    mai 11, 2017 10:35:06 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
    AVERTISSEMENT: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:gestioninscription' did not find a matching property.
    mai 11, 2017 10:35:06 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
    AVERTISSEMENT: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:webstockapplication' did not find a matching property.
    mai 11, 2017 10:35:06 AM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: Server version:        Apache Tomcat/7.0.73
    mai 11, 2017 10:35:06 AM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: Server built:          Nov 7 2016 21:27:23 UTC
    mai 11, 2017 10:35:06 AM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: Server number:         7.0.73.0
    mai 11, 2017 10:35:06 AM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: OS Name:               Windows 8.1
    mai 11, 2017 10:35:06 AM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: OS Version:            6.3
    mai 11, 2017 10:35:06 AM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: Architecture:          amd64
    mai 11, 2017 10:35:06 AM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: Java Home:             C:\Program Files\Java\jre7
    mai 11, 2017 10:35:06 AM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: JVM Version:           1.7.0_75-b13
    mai 11, 2017 10:35:06 AM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: JVM Vendor:            Oracle Corporation
    mai 11, 2017 10:35:06 AM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: CATALINA_BASE:         C:\Users\Adebayo G. Wilfried\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp5
    mai 11, 2017 10:35:06 AM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: CATALINA_HOME:         C:\tomcat7\apache-tomcat-7.0.73
    mai 11, 2017 10:35:06 AM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: Command line argument: -Dcatalina.base=C:\Users\Adebayo G. Wilfried\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp5
    mai 11, 2017 10:35:06 AM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: Command line argument: -Dcatalina.home=C:\tomcat7\apache-tomcat-7.0.73
    mai 11, 2017 10:35:06 AM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: Command line argument: -Dwtp.deploy=C:\Users\Adebayo G. Wilfried\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp5\wtpwebapps
    mai 11, 2017 10:35:06 AM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: Command line argument: -Djava.endorsed.dirs=C:\tomcat7\apache-tomcat-7.0.73\endorsed
    mai 11, 2017 10:35:06 AM org.apache.catalina.startup.VersionLoggerListener log
    INFOS: Command line argument: -Dfile.encoding=Cp1252
    mai 11, 2017 10:35:06 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
    INFOS: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre1.8.0_66/bin/server;C:/Program Files/Java/jre1.8.0_66/bin;C:/Program Files/Java/jre1.8.0_66/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Java\jdk1.7.0_75;C:\wamp\bin\php\php5.5.12;C:\Users\Adebayo G. Wilfried\Desktop\eclipse-jee-kepler-SR2-win32-x86_64\eclipse;;.
    mai 11, 2017 10:35:07 AM org.apache.coyote.AbstractProtocol init
    INFOS: Initializing ProtocolHandler ["http-bio-8080"]
    mai 11, 2017 10:35:07 AM org.apache.coyote.AbstractProtocol init
    INFOS: Initializing ProtocolHandler ["ajp-bio-8009"]
    mai 11, 2017 10:35:07 AM org.apache.catalina.startup.Catalina load
    INFOS: Initialization processed in 2291 ms
    mai 11, 2017 10:35:07 AM org.apache.catalina.core.StandardService startInternal
    INFOS: Démarrage du service Catalina
    mai 11, 2017 10:35:07 AM org.apache.catalina.core.StandardEngine startInternal
    INFOS: Starting Servlet Engine: Apache Tomcat/7.0.73
    mai 11, 2017 10:35:08 AM org.apache.catalina.startup.TldConfig execute
    INFOS: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    mai 11, 2017 10:35:09 AM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
    INFOS: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [171] milliseconds.
    mai 11, 2017 10:35:09 AM org.apache.catalina.startup.TldConfig execute
    INFOS: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    mai 11, 2017 10:35:10 AM org.apache.catalina.startup.TldConfig execute
    INFOS: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    mai 11, 2017 10:35:11 AM org.apache.catalina.startup.TldConfig execute
    INFOS: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    mai 11, 2017 10:35:11 AM org.apache.catalina.startup.TldConfig execute
    INFOS: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    mai 11, 2017 10:35:12 AM org.apache.catalina.startup.TldConfig execute
    INFOS: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    mai 11, 2017 10:35:12 AM org.apache.coyote.AbstractProtocol start
    INFOS: Starting ProtocolHandler ["http-bio-8080"]
    mai 11, 2017 10:35:12 AM org.apache.coyote.AbstractProtocol start
    INFOS: Starting ProtocolHandler ["ajp-bio-8009"]
    mai 11, 2017 10:35:12 AM org.apache.catalina.startup.Catalina start
    INFOS: Server startup in 4718 ms
    null
    mai 11, 2017 10:35:39 AM org.apache.catalina.core.StandardWrapperValve invoke
    GRAVE: "Servlet.service()" pour la servlet ModifierMagasin a généré une exception
    java.lang.NumberFormatException: null
        at java.lang.Long.parseLong(Unknown Source)
        at java.lang.Long.parseLong(Unknown Source)
        at com.form.ModifierMagasinForm.modifierMagasin(ModifierMagasinForm.java:54)
        at com.servlet.ModifierMagasin.doPost(ModifierMagasin.java:43)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:958)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1087)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Unknown Source)
     
    mai 11, 2017 10:57:55 AM org.apache.catalina.core.StandardContext reload
    INFOS: Le rechargement du contexte [/webstockapplication] a démarré
    mai 11, 2017 10:57:55 AM org.apache.catalina.startup.TldConfig execute
    INFOS: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    mai 11, 2017 10:57:56 AM org.apache.catalina.core.StandardContext reload
    INFOS: Le rechargement de ce contexte est terminé
    J'ai une idée d'où peut provenir l'erreur (com.form.ModifierMagasinForm.modifierMagasin(ModifierMagasinForm.java:54), mais j'arrive pas à la corriger. rien de ce que je fais ne marche.

    Merci de bien vouloir me donner un coup de main afin que je puisse continuer ma formation.

  2. #2
    Modérateur
    Avatar de joel.drigo
    Homme Profil pro
    Ingénieur R&D - Développeur Java
    Inscrit en
    Septembre 2009
    Messages
    12 430
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 55
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur R&D - Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2009
    Messages : 12 430
    Billets dans le blog
    2
    Par défaut
    Salut,

    Cette erreur signifie que tu essayes de parser null en long, comme par exemple, dans le code ci-dessous :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    String valeurParametre = getValeurParametre(request, PARAM_ID_MAGASIN);
     
            Magasin magasin = magasinDao.selectionnerMagasinParId(Long.parseLong(valeurParametre));
    si valeurParametre est null.

    La méthode getValeurParametre retourne null si le paramètre n'est pas trouvée dans la requête ou que la valeur du paramètre est vide, ou constituée uniquement de whitespaces. Le nom du paramètre est "idMagasin", donc

    1. il faudrait traiter de toute manière le cas où la valeur est null, parce que ce n'est pas impossible. Le traiter soit en replaçant par une valeur par défaut, soit par exeption, ou httpstatus BAD_REQUEST, ou redirection vers le formulaire, etc.
    2. il faut chercher ce qui invoque cette méthode fournit bien toujours un paramètre de ce nom avec une valeur non vide et constituée d'autre chose que de whitespaces.


    Je ne suis pas un expert de la programmation Web, mais il me semble que le problème vient du fait que le champ de form idMagasin est disabled (et que la valeur ne soit pas soumise en paramètre de la requête de ce fait). Il me semble que la solution ce serait d'utiliser l'attribut readonly (si tu veux afficher le champ, et hidden sinon).
    L'expression "ça marche pas" ne veut rien dire. Indiquez l'erreur, et/ou les comportements attendus et obtenus, et donnez un Exemple Complet Minimal qui permet de reproduire le problème.
    La plupart des réponses à vos questions sont déjà dans les FAQs ou les Tutoriels, ou peut-être dans une autre discussion : utilisez la recherche interne.
    Des questions sur Java : consultez le Forum Java. Des questions sur l'EDI Eclipse ou la plateforme Eclipse RCP : consultez le Forum Eclipse.
    Une question correctement posée et rédigée et vous aurez plus de chances de réponses adaptées et rapides.
    N'oubliez pas de mettre vos extraits de code entre balises CODE (Voir Mode d'emploi de l'éditeur de messages).
    Nouveau sur le forum ? Consultez Les Règles du Club.

  3. #3
    Membre averti
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2014
    Messages
    29
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Bénin

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2014
    Messages : 29
    Par défaut Merci pour la solution
    Merci Joel.drigo.

    Effectivement l'erreur provient du fait que le champ de form idMagasin soit disabled.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 0
    Dernier message: 12/08/2015, 02h31
  2. Exception de type java.lang.NullPointerException
    Par maxi297 dans le forum AWT/Swing
    Réponses: 4
    Dernier message: 14/01/2012, 17h26
  3. Exception java.lang NumberFormatException
    Par sarrajbs dans le forum Struts 1
    Réponses: 5
    Dernier message: 19/08/2010, 00h53
  4. Réponses: 5
    Dernier message: 20/11/2009, 13h18
  5. Exception : java.lang.NumberFormatException
    Par maxlegrand dans le forum Langage
    Réponses: 3
    Dernier message: 06/07/2007, 12h02

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo