Erreur: java.lang.NullPointerException

fabilande Messages postés 4 Date d'inscription mardi 19 mai 2009 Statut Membre Dernière intervention 24 novembre 2011 - 21 nov. 2011 à 16:54
BunoCS Messages postés 15472 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 25 mars 2024 - 24 nov. 2011 à 20:18
Bonjour tout le monde!

J'ai cette erreur qui s'affiche quand j'essaie d'exécuter mon programme.
java.lang.NullPointerException
at etud.etudiantClient.main(etudiantClient.java:35)

Est-ce que quelqu'un sait d'où ça peut provenir s'il vous plait ?

Voilà mon code:
// etudiantClient.java
// mini Client for accessing bean etudiant

package etud;

import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;
import java.rmi.RemoteException;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

/**
 *
 */
public class etudiantClient {
 private Context initialContext = null;
    static Context ctx = null; 
    static etudiantHome home = null;

    public static void main(String[] arg)
    {
//     Get InitialContext
    	/*try {
    	    ctx = getInitialContext();
    	} catch (NamingException e) {
    	    e.printStackTrace();
    	    System.exit(2);
    	}*/
      // Lookup bean home
      String bName = "etudiantHome";
      try {
          home = (etudiantHome) PortableRemoteObject.narrow(ctx.lookup(bName), etudiantHome.class);           
          // Création de deux clients dans la base de données
          etudiant et = home.create(Integer.valueOf(28),"Durant","Paul");
           
               
            // Objet de type client pour faire nos tests
            etudiant test;
     
            // Recherche d'un client par Primary key
            test  =  home.findByPrimaryKey(Integer.valueOf(9));
            System.out.println("Nom du client 9 : " + test.getNom());
            test.setNom("client9encore");
            // Recherche par le nom de la personne
            System.out.println("Liste des clients qui ont pour nom Durant :");
                Collection cl = home.findByNom("Durant");
                Iterator iLast = cl.iterator();
                while (iLast.hasNext()) {
                  test = (etudiant)iLast.next();
                  System.out.println("->" + test.getPrenom());
                }
     
                // Création et suppression d'un client
                test  =  home.create(Integer.valueOf(29),"TEST3", "TEST3");
                
                test.setNom("NOUVEAUTEST3");
                System.out.println(test.getNom()+" - "+test.getPrenom());
                test.remove();
           
                // Modification d'une donnée membre d'un EJB
                et.setPrenom("Jean");
      } catch(Exception e) {
          e.printStackTrace();
          System.exit(2);
      }
    }

private static Context getInitialContext() {
// TODO Auto-generated method stub
return null;
}
    
}





Lande
A voir également:

9 réponses

cs_GodConan Messages postés 2113 Date d'inscription samedi 8 novembre 2003 Statut Contributeur Dernière intervention 6 octobre 2012 12
22 nov. 2011 à 00:34
salut,

quand tu fais ce genre de demande donne aussi le message d erreur.. qui comporte surement la ligne incriminée....

et un nullpointer ;o) c est qd meme asser simple ;o) ... ca veut tout simplement dire que tu pointe sur une valeur null ;o) ...
en locurrence surement "ctx.lookup(bName) " ... je ne vois pas ds ton code l initialisation de ctx



GodConan ;o)
0
BunoCS Messages postés 15472 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 25 mars 2024 103
22 nov. 2011 à 14:06
Hello,
java.lang.NullPointerException
at etud.etudiantClient.main(etudiantClient.java:35)

--> Regarde la ligne 35.

@+
Buno
----------------------------------------
L'urgent est fait, l'impossible est en cours. Pour les miracles, prévoir un délai...
0
cs_GodConan Messages postés 2113 Date d'inscription samedi 8 novembre 2003 Statut Contributeur Dernière intervention 6 octobre 2012 12
22 nov. 2011 à 18:29
;o) ... ben c est ce que je dis ;o) probleme de ctx ;o) ... une simle lecture suffit ;o) héhé

GodConan ;o)
0
fabilande Messages postés 4 Date d'inscription mardi 19 mai 2009 Statut Membre Dernière intervention 24 novembre 2011
22 nov. 2011 à 19:09
Merci pour vos commentaires. Mais j'ai fait l'initialisation ici
 private Context initialContext = null;
    static Context ctx = null; 
    static etudiantHome home = null;


J'arrive toujours pas à résoudre mon problème. J'attends que vous m'aidiez s'il vous plait.

Lande
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
BunoCS Messages postés 15472 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 25 mars 2024 103
22 nov. 2011 à 20:24
Attention,
Ne pas confondre:
- déclaration (ce que tu as effectivement fait)
- initialisation: ne serait-ce pas le code en commentaire?


@+
Buno
----------------------------------------
L'urgent est fait, l'impossible est en cours. Pour les miracles, prévoir un délai...
0
fabilande Messages postés 4 Date d'inscription mardi 19 mai 2009 Statut Membre Dernière intervention 24 novembre 2011
24 nov. 2011 à 17:17
Quand j'enlève le commentaire, il souligne une erreur et me mets ces lignes ci-dessous quand je pointe ma cursseur sur la ligne:
Unreachable catch block for NamingException. This exception is never thrown from the try statement body.

void java.lang.Throwable.printStackTrace()
Prints this throwable and its backtrace to the standard error stream. This method prints a stack trace for this Throwable object on the error output stream that is the value of the field System.err. The first line of output contains the result of the toString() method for this object. Remaining lines represent data previously recorded by the method fillInStackTrace(). The format of this information depends on the implementation, but the following example may be regarded as typical:

 java.lang.NullPointerException
         at MyClass.mash(MyClass.java:9)
         at MyClass.crunch(MyClass.java:6)
         at MyClass.main(MyClass.java:3)
 
This example was produced by running the program: 
 class MyClass {
     public static void main(String[] args) {
         crunch(null);
     }
     static void crunch(int[] a) {
         mash(a);
     }
     static void mash(int[] b) {
         System.out.println(b[0]);
     }
 }
 
The backtrace for a throwable with an initialized, non-null cause should generally include the backtrace for the cause. The format of this information depends on the implementation, but the following example may be regarded as typical: 
 HighLevelException: MidLevelException: LowLevelException
         at Junk.a(Junk.java:13)
         at Junk.main(Junk.java:4)
 Caused by: MidLevelException: LowLevelException
         at Junk.c(Junk.java:23)
         at Junk.b(Junk.java:17)
         at Junk.a(Junk.java:11)
         ... 1 more
 Caused by: LowLevelException
         at Junk.e(Junk.java:30)
         at Junk.d(Junk.java:27)
         at Junk.c(Junk.java:21)
         ... 3 more
 
Note the presence of lines containing the characters "...". These lines indicate that the remainder of the stack trace for this exception matches the indicated number of frames from the bottom of the stack trace of the exception that was caused by this exception (the "enclosing" exception). This shorthand can greatly reduce the length of the output in the common case where a wrapped exception is thrown from same method as the "causative exception" is caught. The above example was produced by running the program: 
 public class Junk {
     public static void main(String args[]) {
         try {
             a();
         } catch(HighLevelException e) {
             e.printStackTrace();
         }
     }
     static void a() throws HighLevelException {
         try {
             b();
         } catch(MidLevelException e) {
             throw new HighLevelException(e);
         }
     }
     static void b() throws MidLevelException {
         c();
     }
     static void c() throws MidLevelException {
         try {
             d();
         } catch(LowLevelException e) {
             throw new MidLevelException(e);
         }
     }
     static void d() throws LowLevelException {
        e();
     }
     static void e() throws LowLevelException {
         throw new LowLevelException();
     }
 }

 class HighLevelException extends Exception {
     HighLevelException(Throwable cause) { super(cause); }
 }

 class MidLevelException extends Exception {
     MidLevelException(Throwable cause)  { super(cause); }
 }

 class LowLevelException extends Exception {
 }



Alors je ne comprends pas trop ce qu'il faut faire. Merci pour votre remarque.
0
BunoCS Messages postés 15472 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 25 mars 2024 103
24 nov. 2011 à 17:38
Hello,
Cela veut dire, tout simplement que tu ne recevras jamais d'exception de type NamingException. 2 possibilités:
- soit tu généralise, en remplaçant NamingException par Exception tout court
- soit tu enlèves le try..catch, en gardant le code d'init, bien sûr

Note: En règle générale, la méthode de "je commente jusqu'à ce que ça compile" ne fonctionne que très rarement. Autant se pencher tout de suite sur les vrais problèmes.

@+
Buno
----------------------------------------
L'urgent est fait, l'impossible est en cours. Pour les miracles, prévoir un délai...
0
fabilande Messages postés 4 Date d'inscription mardi 19 mai 2009 Statut Membre Dernière intervention 24 novembre 2011
24 nov. 2011 à 17:59
Merci pour votre aide. ça me renvoie encore le code d'erreur apres avoir fait les deux options.
java.lang.NullPointerException
at etud.etudiantClient.main(etudiantClient.java:34)


Merci encore.
0
BunoCS Messages postés 15472 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 25 mars 2024 103
24 nov. 2011 à 20:18
Oui, mais là, c'est pour la variable de type étudiantHome car elle non plus n'est pas initialisée
New, ça te dit quelque chose? ;-)
@+
Buno
----------------------------------------
L'urgent est fait, l'impossible est en cours. Pour les miracles, prévoir un délai...
0
Rejoignez-nous