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

Spring Java Discussion :

Erreur "Could not instantiate bean class []: Constructor threw exception"


Sujet :

Spring Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2012
    Messages
    91
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2012
    Messages : 91
    Par défaut Erreur "Could not instantiate bean class []: Constructor threw exception"
    Bonjour,

    Je suis en train de m'initier à Spring et donc j'ai créé un projet test.

    Le problème est que j'obtiens cette exception à l'exécution :
    Could not instantiate bean class [monProg.maClasse]: Constructor threw exception;
    Quelqu'un pourrait m'éclairer sur la manière de résoudre ce problème ?

    Merci d'avance pour votre aide.

  2. #2
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Par défaut
    Bonjour,

    Peut-on voir un peu plus sur les messages d'erreur? Les codes java et xml que tu as fait correspondants à la classe dite par l'erreur?

    A+.

  3. #3
    Membre confirmé
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2012
    Messages
    91
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2012
    Messages : 91
    Par défaut
    Voilà le message d'erreur :
    Exception in Application start method
    java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.javafx.main.Main.launchApp(Main.java:642)
    at com.javafx.main.Main.main(Main.java:805)
    Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.access$000(Unknown Source)
    at com.sun.javafx.application.LauncherImpl$1.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:722)
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clav' defined in class path resource [melordi/springXMLConfig.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [melordi.Clavier]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:288)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1035)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:939)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at melordi.MelOrdi.start(MelOrdi.java:27)
    at com.sun.javafx.application.LauncherImpl$5.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$5.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$4.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$3$1.run(Unknown Source)
    ... 1 more
    Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [melordi.Clavier]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:162)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:110)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:280)
    ... 21 more
    Caused by: java.lang.NullPointerException
    at melordi.Clavier.<init>(Clavier.java:62)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
    ... 23 more
    Le code XML :
    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
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:aop="http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
           xmlns:context="http://www.springframework.org/schema/context/spring-context-2.5.xsd"
           xmlns:lang="http://www.springframework.org/schema/lang/spring-lang-2.5.xsd"
           xmlns:util="http://www.springframework.org/schema/util/spring-util-2.5.xsd"
           xmlns:p="http://www.springframework.org/schema/p"
     
           xsi:schemaLocation="http://www.springframework.org/schema/beans <a href="http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" target="_blank">http://www.springframework.org/schem...-beans-2.5.xsd</a>
              <a href="http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" target="_blank">http://www.springframework.org/schem...ng-aop-2.5.xsd</a> <a href="http://www.springframework.org/schema/aop/spring-aop-2.5.xsd/spring-spring-aop-2.5.xsd-3.1.1.RELEASE.xsd" target="_blank">http://www.springframework.org/schem....1.RELEASE.xsd</a>
              <a href="http://www.springframework.org/schema/context/spring-context-2.5.xsd" target="_blank">http://www.springframework.org/schem...ontext-2.5.xsd</a> <a href="http://www.springframework.org/schema/context/spring-context-2.5.xsd/spring-spring-context-2.5.xsd-3.1.1.RELEASE.xsd" target="_blank">http://www.springframework.org/schem....1.RELEASE.xsd</a>
              <a href="http://www.springframework.org/schema/lang/spring-lang-2.5.xsd" target="_blank">http://www.springframework.org/schem...g-lang-2.5.xsd</a> <a href="http://www.springframework.org/schema/lang/spring-lang-2.5.xsd/spring-spring-lang-2.5.xsd-3.1.1.RELEASE.xsd" target="_blank">http://www.springframework.org/schem....1.RELEASE.xsd</a>
              <a href="http://www.springframework.org/schema/util/spring-util-2.5.xsd" target="_blank">http://www.springframework.org/schem...g-util-2.5.xsd</a> <a href="http://www.springframework.org/schema/util/spring-util-2.5.xsd/spring-spring-util-2.5.xsd-3.1.1.RELEASE.xsd" target="_blank">http://www.springframework.org/schem....1.RELEASE.xsd</a>
    ">
        <bean id="sound" class="melordi.SoundPlayer">
        </bean>
     
        <bean id="metr" class="melordi.Metronome">
        </bean>
     
        <bean id="change" class="melordi.ChangementSound">
            <constructor-arg index="0" ref="sound"/>
        </bean>
     
        <bean id="clav" class="melordi.Clavier">
            <constructor-arg index="0" ref="sound"/>
            <constructor-arg index="1" ref="metr"/>
        </bean>
     
        <bean id="vol" class="melordi.Volume">
            <constructor-arg index="0" ref="clav"/>
            <constructor-arg index="1" ref="sound"/>
        </bean>
     
        <bean id="touch" class="melordi.Touche" scope="prototype">
            <constructor-arg index="0" ref="sound"/>
            <constructor-arg index="1" ref="metr"/>
            <property name="haut" value="70"/>
            <property name="larg" value="70"/>
        </bean>
    </beans>
    J'utilise JavaFX. La méthode start de la classe principale est comme suit :
    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
    public void start(Stage primaryStage) {
            primaryStage.setTitle("MelOrdi - Vamos la musica !");
     
            appCtx = new ClassPathXmlApplicationContext("melordi/springXMLConfig.xml");
     
            Group backGround = new Group();
     
            Scene scene = new Scene(backGround, 500, 500, Color.WHITE);
     
            SoundPlayer instrument = appCtx.getBean("sound", SoundPlayer.class);
     
            Metronome metr = appCtx.getBean("metr", Metronome.class);
            backGround.getChildren().add(metr);
     
            Clavier monClavier = appCtx.getBean("clav", Clavier.class);
            backGround.getChildren().add(monClavier);
     
            ChangementSound change = appCtx.getBean("change", ChangementSound.class);
            backGround.getChildren().add(change);
     
            Volume volume = appCtx.getBean("vol", Volume.class);
            backGround.getChildren().add(volume);
            monClavier.setVol(volume);
     
            primaryStage.setScene(scene);
     
            primaryStage.show();
     
            monClavier.requestFocus();  //transmission de la focalisation
        }
    Enfin le classe qui crée l'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
    private SoundPlayer sound;
        private Volume vol;
        private Metronome metr;
     
        private ApplicationContext appCtx;
     
        public Clavier(SoundPlayer sound, Metronome metr){
     
            this.sound = sound;
            this.metr = metr;
     
            Rectangle arriereClavier = new Rectangle(480, 200, Color.BLACK);
            arriereClavier.setArcHeight(30);
            arriereClavier.setArcWidth(30);
     
            arriereClavier.setFill(                 // ajout d'un dégradé
                new LinearGradient(0f, 0f, 0f, 1f, true, CycleMethod.NO_CYCLE,
                    new Stop[] {
                        new Stop(0, Color.web("#333333")),
                        new Stop(1, Color.web("#000000"))
                    }
                )
            );
     
            Reflection reflect = new Reflection(1, 0.25, 0, 0.5);// application d'un effet de réflection
            arriereClavier.setEffect(reflect);
     
            this.setTranslateX(10);
            this.setTranslateY(250);
     
            this.getChildren().add(arriereClavier);
     
            Touche touche = appCtx.getBean("touch", Touche.class);
            int var = -5;
            int dep = (int)(arriereClavier.getWidth() - (8 * (touche.getLarg() + 5))) / 2;
            for (int i = 0; i < 8; i++){
                touches[i] = new Touche(notes[i], i * (touche.getLarg() + 5) + dep, 100 + var, sons[i], raccourcis[i], sound, metr);
                this.getChildren().add(touches[i]);
                var = - var;
            }
     
            this.setOnKeyPressed(new EventHandler<KeyEvent>(){
                public void handle(KeyEvent ke) {
                    if (ke.getCode().equals(KeyCode.UP)) {
                        changeVolume(true);
                    }
     
                    if (ke.getCode().equals(KeyCode.DOWN)) {
                        changeVolume(false);
                    }
     
                    for (Touche t : touches){
                        if (t.getRaccourci().toUpperCase().equals(ke.getText().toUpperCase())){
                            t.appui();
                            break;
                        }
                    }
                }
            });
     
            this.setOnKeyReleased(new EventHandler<KeyEvent>(){
                public void handle(KeyEvent ke) {
                    for (Touche t : touches){
                        if (t.getRaccourci().toUpperCase().equals(ke.getText().toUpperCase())){
                            t.relachement();
                            break;
                        }
                    }
                }
            });
        };

  4. #4
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Par défaut
    Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [melordi.Clavier]: Constructor threw exception; nested exception is java.lang.NullPointerException
    Il n'arrive pas à instancier la classe Clavier. Peut-on le voir?

    A+.

  5. #5
    Membre confirmé
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2012
    Messages
    91
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2012
    Messages : 91
    Par défaut
    Voilà la classe Clavier entière :
    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
    import javafx.event.EventHandler;
    import javafx.scene.Parent;
    import javafx.scene.effect.Reflection;
    import javafx.scene.input.KeyCode;
    import javafx.scene.input.KeyEvent;
    import javafx.scene.paint.Color;
    import javafx.scene.paint.CycleMethod;
    import javafx.scene.paint.LinearGradient;
    import javafx.scene.paint.Stop;
    import javafx.scene.shape.Rectangle;
    import org.springframework.context.ApplicationContext;
     
    /**
     *
     * @author fnac
     */
    public class Clavier extends Parent{
     
        private String[] notes = {"Do", "Ré", "Mi", "Fa", "Sol", "La", "Si", "Do"};
        private int[] sons = {60, 62, 64, 65, 67, 69, 71, 72};
        private Touche[] touches = new Touche[8];
        private String[] raccourcis = {"A", "Z", "E", "R", "T", "Y", "U", "I"};
        private SoundPlayer sound;
        private Volume vol;
        private Metronome metr;
     
        private ApplicationContext appCtx;
     
        public Clavier(SoundPlayer sound, Metronome metr){
     
            this.sound = sound;
            this.metr = metr;
     
            Rectangle arriereClavier = new Rectangle(480, 200, Color.BLACK);
            arriereClavier.setArcHeight(30);
            arriereClavier.setArcWidth(30);
     
            arriereClavier.setFill(                 // ajout d'un dégradé
                new LinearGradient(0f, 0f, 0f, 1f, true, CycleMethod.NO_CYCLE,
                    new Stop[] {
                        new Stop(0, Color.web("#333333")),
                        new Stop(1, Color.web("#000000"))
                    }
                )
            );
     
            Reflection reflect = new Reflection(1, 0.25, 0, 0.5);// application d'un effet de réflection
            arriereClavier.setEffect(reflect);
     
            this.setTranslateX(10);
            this.setTranslateY(250);
     
            this.getChildren().add(arriereClavier);
     
            Touche touche = appCtx.getBean("touch", Touche.class);
            int var = -5;
            int dep = (int)(arriereClavier.getWidth() - (8 * (touche.getLarg() + 5))) / 2;
            for (int i = 0; i < 8; i++){
                touches[i] = new Touche(notes[i], i * (touche.getLarg() + 5) + dep, 100 + var, sons[i], raccourcis[i], sound, metr);
                this.getChildren().add(touches[i]);
                var = - var;
            }
     
            this.setOnKeyPressed(new EventHandler<KeyEvent>(){
                public void handle(KeyEvent ke) {
                    if (ke.getCode().equals(KeyCode.UP)) {
                        changeVolume(true);
                    }
     
                    if (ke.getCode().equals(KeyCode.DOWN)) {
                        changeVolume(false);
                    }
     
                    for (Touche t : touches){
                        if (t.getRaccourci().toUpperCase().equals(ke.getText().toUpperCase())){
                            t.appui();
                            break;
                        }
                    }
                }
            });
     
            this.setOnKeyReleased(new EventHandler<KeyEvent>(){
                public void handle(KeyEvent ke) {
                    for (Touche t : touches){
                        if (t.getRaccourci().toUpperCase().equals(ke.getText().toUpperCase())){
                            t.relachement();
                            break;
                        }
                    }
                }
            });
        };
     
        public void setVol(Volume vol){
            this.vol = vol;
        }
     
        public void changeVolume(boolean up){
            if (up) {
                this.vol.slider.setValue(this.vol.slider.getValue() + 5);
            }
            else {
                this.vol.slider.setValue(this.vol.slider.getValue() - 5);
            }
        }
     
        public SoundPlayer getSound() {
            return sound;
        }
     
        public void setSound(SoundPlayer sound) {
            this.sound = sound;
        }
     
        public Metronome getMetr() {
            return metr;
        }
     
        public void setMetr(Metronome metr) {
            this.metr = metr;
        }
     
    }
    En fait, j'aimerais pouvoir définir les dimensions des objets dans le fichier XML et les récupérer comme paramètres dans mon code.
    Mais puisque je ne trouve rien du genre "getProperty()", je veux passer par un bean mais je ne sais pas comment m'y prendre.

Discussions similaires

  1. Réponses: 1
    Dernier message: 19/03/2012, 16h39
  2. Réponses: 2
    Dernier message: 23/05/2007, 11h40
  3. Réponses: 1
    Dernier message: 22/11/2006, 12h21
  4. JDIC : Could not find main class
    Par seiryujay dans le forum API standards et tierces
    Réponses: 32
    Dernier message: 08/02/2006, 16h44

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