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 :

[Débutant] Problème java.lang.NullPointerException


Sujet :

Développement Web en Java

  1. #1
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2013
    Messages
    174
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val d'Oise (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juin 2013
    Messages : 174
    Points : 51
    Points
    51
    Par défaut [Débutant] Problème java.lang.NullPointerException
    Bonjour ,

    Voila quand j’exécute mon application depuis ma servlet Inscription ca me donne cette erreur sur ma console :

    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
    SEVERE: "Servlet.service()" pour la servlet com.tomo.servlets.Inscription a généré une exception
    java.lang.NullPointerException
    	at com.tomo.forms.TomodachiForm.formTomodachi(TomodachiForm.java:86)
    	at com.tomo.servlets.Inscription.doPost(Inscription.java:68)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    	at java.lang.Thread.run(Unknown Source)

    J'ai vus que le problème viens de ma méthode (que j'ai situé dans le Form TomodachiForm) d'ajout dans la base de donnée creerTomodachi , j'ai parcourus toutes les pages de l'application vérifié tout mais toujours le même problème , je ne vois pas d'ou peut venir l'erreur ! voila les codes que j'utilise :

    => Servlet Inscription :

    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
    public class Inscription extends HttpServlet {
     
     
        private static final String vue_inscription          = "/WEB-INF/inscription.jsp";
        private static final String vue_informationTomodachi = "/WEB-INF/informationsTomo.jsp";
     
        private static final String tomodachiForm            = "formTomo";
        private static final String tomodachiBean            = "beanTomo";
     
        private static final String sessionTomodachi         = "TomodachiSession";
     
        private TomodachiDAO        tomodachiDAO;
     
        public static final String  InisialDaoFactory        = "daoFactory";
     
        public Inscription() {
            super();
            // TODO Auto-generated constructor stub
        }
     
        public void init( ServletException exception0 ) throws ServletException {
     
            this.tomodachiDAO = ( (DAOFactory) getServletContext().getAttribute( InisialDaoFactory ) ).getTomodachiDAO();
        }
     
     
        protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException,
                IOException {
            this.getServletContext().getRequestDispatcher( vue_inscription ).forward( request, response );
        }
     
     
        protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException,
                IOException {
     
            TomodachiForm formT = new TomodachiForm( tomodachiDAO );
            Tomodachi tomodachi = formT.formTomodachi( request );
     
            request.setAttribute( tomodachiForm, formT );
            request.setAttribute( tomodachiBean, tomodachi );
     
            HttpSession session = request.getSession();
     
            if ( formT.getErreurs().isEmpty() ) {
     
                Map<Long, Tomodachi> mapTomo = (HashMap<Long, Tomodachi>) session.getAttribute( sessionTomodachi );
     
                if ( mapTomo == null ) {
                    mapTomo = new HashMap<Long, Tomodachi>();
                }
     
                mapTomo.put( tomodachi.getId_tomodachi(), tomodachi );
     
                session.setAttribute( sessionTomodachi, mapTomo );
     
                this.getServletContext().getRequestDispatcher( vue_informationTomodachi ).forward( request, response );
     
            } else {
     
                this.getServletContext().getRequestDispatcher( vue_inscription ).forward( request, response );
            }
        }
    }

    => Form TomodachiForm :

    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
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    public class TomodachiForm {
     
        // Attribus :
     
        private String             champ_nom          = "nomT";
        private String             champ_prenom       = "prenomT";
        private String             champ_age          = "AgeT";
        private String             champ_passe        = "passeT";
        private String             champ_confirmation = "confirmationT";
        private String             champ_email        = "emailT";
        private String             champ_adresse      = "adresseT";
        private String             champ_ville        = "villesT";
        private String             champ_apropos      = "aproposT";
     
        private String             forma_date         = "YYYY/MM/dd HH:MM:SS";
     
        public Map<String, String> erreurs            = new HashMap<String, String>();
     
        public String              resultats;
     
        public TomodachiDAO        tomodachiDAO;
     
        public TomodachiForm( TomodachiDAO tomodachiDAO ) {
            super();
            this.tomodachiDAO = tomodachiDAO;
        }
     
        public Map<String, String> getErreurs() {
            return erreurs;
        }
     
        public String getResultats() {
            return resultats;
        }
     
        // Methode principal :
     
        public Tomodachi formTomodachi( HttpServletRequest request ) {
     
            String nom = getvalueChamp( request, champ_nom );
            String prenom = getvalueChamp( request, champ_prenom );
            String age = getvalueChamp( request, champ_age );
            String passe = getvalueChamp( request, champ_passe );
            String confirmation = getvalueChamp( request, champ_confirmation );
            String email = getvalueChamp( request, champ_email );
            String adresse = getvalueChamp( request, champ_adresse );
            String ville = getvalueChamp( request, champ_ville );
            String apropos = getvalueChamp( request, champ_apropos );
     
            Tomodachi tomodachi = new Tomodachi();
     
            traiterNom( nom, tomodachi );
            traiterPrenom( prenom, tomodachi );
            traiterAge( age, tomodachi );
            traiterPasse( passe, confirmation, tomodachi );
            traiterEmail( email, tomodachi );
            traiterVille( ville, tomodachi );
            tomodachi.setAdresse( adresse );
            tomodachi.setApropos( apropos );
     
            DateTime dt = new DateTime( DateTimeZone.forID( "Africa/Casablanca" ) );
            DateTimeFormatter forma = DateTimeFormat.forPattern( forma_date );
            String date = dt.toString( forma );
     
            tomodachi.setDate_inscription( date );
     
            try {
     
                if ( erreurs.isEmpty() ) {
                    tomodachiDAO.creerTomodachi( tomodachi );
                    resultats = "Inscription réussite";
                } else {
                    resultats = "Inscription échoué";
                }
     
            } catch ( DAOException e ) {
     
                resultats = "Erreur dans la creation du tomosachi";
                e.printStackTrace();
                setErreurs( "erreur", "Erreur imprévue dans la création du client" );
            }
     
            return tomodachi;
        }
     
        // methodes utilitaires :
     
        public void traiterVille( String ville, Tomodachi tomodachi ) {
     
            try {
     
                validationVille( ville );
     
            } catch ( FormsException e ) {
                setErreurs( champ_ville, e.getMessage() );
            }
            tomodachi.setVille( ville );
        }
     
        public void traiterEmail( String email, Tomodachi tomodachi ) {
            try {
     
                validationEmail( email );
     
            } catch ( FormsException e ) {
                setErreurs( champ_email, e.getMessage() );
            }
            tomodachi.setEmail( email );
        }
     
        public void traiterPasse( String motpasse, String confirm, Tomodachi tomodachi ) {
            try {
     
                validationPasse( motpasse, confirm );
     
            } catch ( FormsException e ) {
                setErreurs( champ_passe, e.getMessage() );
                setErreurs( champ_confirmation, null );
            }
            tomodachi.setMot_de_passe( motpasse );
        }
     
        public void traiterAge( String age, Tomodachi tomodachi ) {
            try {
     
                validationAge( age );
     
            } catch ( FormsException e ) {
                setErreurs( champ_age, e.getMessage() );
            }
            tomodachi.setAge( age );
        }
     
        public void traiterPrenom( String prenom, Tomodachi tomodachi ) {
            try {
     
                validationPrenom( prenom );
     
            } catch ( FormsException e ) {
                setErreurs( champ_prenom, e.getMessage() );
            }
            tomodachi.setPrenom( prenom );
        }
     
        public void traiterNom( String nom, Tomodachi tomodachi ) {
            try {
     
                validationNom( nom );
     
            } catch ( FormsException e ) {
                setErreurs( champ_nom, e.getMessage() );
            }
            tomodachi.setNom( nom );
        }
     
        public void validationVille( String ville ) throws FormsException {
     
            if ( ville == null ) {
                throw new FormsException( "Veuillez choisir votre ville" );
            }
     
        }
     
        public void validationEmail( String email ) throws FormsException {
            if ( email != null ) {
                if ( !email.matches( "([^.@]+)(\\.[^.@]+)*@([^.@]+\\.)+([^.@]+)" ) ) {
                    throw new FormsException( "L'adresse email n'est pas valide" );
                }
            } else {
                throw new FormsException( "Veuillez entrer votre adresse email" );
            }
        }
     
        public void validationPasse( String motpasse, String confir ) throws FormsException {
            if ( motpasse != null && confir != null ) {
                if ( !motpasse.equals( confir ) ) {
                    throw new FormsException( "Le mot de passe et la confirmation sont differents" );
                }
                if ( motpasse.length() < 3 ) {
                    throw new FormsException( "Le mot de passe doit contenir plus de 3 caractères" );
                }
     
            } else {
                throw new FormsException( "Veuillez entrer un mot de passe et sa confirmation" );
            }
        }
     
        public void validationAge( String age ) throws FormsException {
     
            if ( age != null ) {
                if ( !age.matches( "^\\d+$" ) ) {
                    throw new FormsException( "L'age doit etre numérique" );
                }
                if ( age.length() > 3 ) {
                    throw new FormsException( "L'age doit contenir 3 chiffres maximum" );
                }
            } else {
                throw new FormsException( "Veuillez entrer votre age" );
            }
     
        }
     
        public void validationPrenom( String prenom ) throws FormsException {
     
            if ( prenom == null ) {
                throw new FormsException( "Veuillez entrer un prenom" );
            }
     
        }
     
        public void validationNom( String nom ) throws FormsException {
     
            if ( nom == null ) {
                throw new FormsException( "Veuillez entrer un nom" );
            }
     
        }
     
        public static String getvalueChamp( HttpServletRequest request, String champ ) {
     
            String value = request.getParameter( champ );
     
            if ( value == null || value.trim().length() == 0 ) {
                return null;
            } else {
                return value.trim();
            }
     
        }
     
        public void setErreurs( String champ, String message ) {
            erreurs.put( champ, message );
        }
     
    }
    (dao)
    => Interface TomodachiDAO :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    public interface TomodachiDAO {
     
        void creerTomodachi( Tomodachi tomodachi ) throws DAOException;
    }
    =>Implementation de la methode creerTomodachi TomodachiDAOimplement :

    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
    public class TomodachiDAOimplement implements TomodachiDAO {
     
        public static final String requeteInsert = "insert into tomodachi(nom,prenom,age,dateInscription,motDePasse,email,adresse,ville,apropos) values(?,?,?,?,?,?,?,?,?)";
     
        private DAOFactory         daoFactory;
     
        public TomodachiDAOimplement( DAOFactory daoFactory ) {
            super();
            this.daoFactory = daoFactory;
        }
     
        @Override
        public void creerTomodachi( Tomodachi tomoda ) throws DAOException {
            Connection connection = null;
            PreparedStatement prepastatement = null;
            ResultSet Keygen = null;
     
            try {
     
                connection = daoFactory.getConnection();
                prepastatement = inisialisationPrepareStatement( connection, requeteInsert, true, tomoda.getNom(),
                        tomoda.getPrenom(), tomoda.getAge(), tomoda.getDate_inscription(), tomoda.getMot_de_passe(),
                        tomoda.getEmail(), tomoda.getAdresse(), tomoda.getVille(), tomoda.getApropos() );
     
                int status = prepastatement.executeUpdate();
     
                if ( status == 0 ) {
                    throw new DAOException( "Aucune ligne n'as etais ajouter" );
                }
                Keygen = prepastatement.getGeneratedKeys();
     
                if ( Keygen.next() ) {
                    tomoda.setId_tomodachi( Keygen.getLong( 1 ) );
                } else {
                    throw new DAOException( "Aucune cle du tomodachi n'as etais cree" );
                }
     
            } catch ( SQLException e ) {
                throw new DAOException( "Insertion du Tomodachi echouer ! " + e );
            } finally {
                fermeture( Keygen, prepastatement, connection );
            }
        }
     
    }
    => 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
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    public class DAOFactory {
     
        private static String fichier_proprieter  = "/com/tomo/dao/dao.properties";
     
        private static String prop_url            = "url";
        private static String prop_driver         = "driver";
        private static String prop_nomUtilisateur = "nomutilisateur";
        private static String prop_motdepasse     = "motdepasse";
     
        private String        url;
        private String        utilisateur;
        private String        mot_passe;
     
        public DAOFactory( String url, String utilisateur, String mot_passe ) {
            super();
            this.url = url;
            this.utilisateur = utilisateur;
            this.mot_passe = mot_passe;
        }
     
        /*
         * private static BoneCP boneCP;
         * 
         * public DAOFactory( BoneCP boneCP ) { super(); this.boneCP = boneCP; }
         */
        public static DAOFactory getInstance() throws DAOConfigurationException {
     
            Properties properties = new Properties();
     
            String url;
            String driver;
            String nomUtilisateur;
            String motDePasse;
     
            ClassLoader classe = Thread.currentThread().getContextClassLoader();
            InputStream fichier = classe.getResourceAsStream( fichier_proprieter );
     
            if ( fichier == null ) {
                throw new DAOConfigurationException( "Le fichier " + fichier_proprieter + " introuvable" );
            }
     
            try {
     
                properties.load( fichier );
                url = properties.getProperty( prop_url );
                driver = properties.getProperty( prop_driver );
                nomUtilisateur = properties.getProperty( prop_nomUtilisateur );
                motDePasse = properties.getProperty( prop_motdepasse );
     
            } catch ( IOException e ) {
                throw new DAOConfigurationException( "Le fichier " + fichier_proprieter + " n'as pas étais charger " + e );
            }
     
            try {
     
                Class.forName( driver );
     
            } catch ( ClassNotFoundException e ) {
                throw new DAOConfigurationException( "Chargement du driver echoue " + e );
            }
     
            /*
             * try {
             * 
             * BoneCPConfig boneConfig = new BoneCPConfig();
             * 
             * boneConfig.setJdbcUrl( url ); boneConfig.setUsername( nomUtilisateur
             * ); boneConfig.setPassword( motDePasse );
             * 
             * boneConfig.setMinConnectionsPerPartition( 5 );
             * boneConfig.setMaxConnectionsPerPartition( 10 );
             * boneConfig.setPartitionCount( 2 );
             * 
             * boneCP = new BoneCP( boneConfig );
             * 
             * } catch ( SQLException e ) { e.printStackTrace(); throw new
             * DAOConfigurationException(
             * "Erreur de configuration du pool de connexions " + e ); }
             */
     
            DAOFactory instance = new DAOFactory( url, nomUtilisateur, motDePasse );
     
            return instance;
        }
     
        Connection getConnection() throws SQLException {
            return DriverManager.getConnection( url, utilisateur, mot_passe );
     
        }
     
        public TomodachiDAO getTomodachiDAO() {
            return new TomodachiDAOimplement( this );
     
        }
    }
    Et voila aussi l'image d'erreur de mon navigateur , merci .
    Images attachées Images attachées  

  2. #2
    Membre régulier
    Homme Profil pro
    Inscrit en
    Février 2009
    Messages
    37
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2009
    Messages : 37
    Points : 71
    Points
    71
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    java.lang.NullPointerException at com.tomo.forms.TomodachiForm.formTomodachi(TomodachiForm.java:86)
    Quelle est l'instruction à cette ligne ?

  3. #3
    Membre actif
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Septembre 2011
    Messages
    196
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2011
    Messages : 196
    Points : 242
    Points
    242
    Par défaut
    Vérifie que ton DAO tomodachiDAO ne soit pas null car à mon avis c'est l'instruction suivante qui doit planter :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    tomodachiDAO.creerTomodachi( tomodachi );

Discussions similaires

  1. Problème java.lang.NullPointerException : null null
    Par info20111 dans le forum Général Java
    Réponses: 2
    Dernier message: 12/06/2013, 17h34
  2. Problème java.lang.NullPointerException
    Par ThE BiShOp* dans le forum Débuter avec Java
    Réponses: 11
    Dernier message: 29/05/2012, 09h51
  3. [Eclipse 3.2.2] Problème java.lang.NullPointerException
    Par richard_sraing dans le forum Eclipse Java
    Réponses: 1
    Dernier message: 01/03/2011, 16h51
  4. Problème "java.lang.NullPointerException"
    Par driz764 dans le forum Débuter avec Java
    Réponses: 10
    Dernier message: 22/10/2008, 18h33
  5. [Débutant] Erreur java.lang.NullPointerException
    Par Kevin12 dans le forum Struts 1
    Réponses: 2
    Dernier message: 12/02/2007, 15h48

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