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

JavaFX Discussion :

null pointer sur un @FXML


Sujet :

JavaFX

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    dev
    Inscrit en
    Octobre 2020
    Messages
    25
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : dev

    Informations forums :
    Inscription : Octobre 2020
    Messages : 25
    Par défaut null pointer sur un @FXML
    Bonjour, Bonsoir
    j'aurai besoin d'aide sur un truc que je trouve un peu bizarre. J'ai une ListView dans une fenêtre fait avec SceneBuilder, je lui est mis l'id "listView" et quand je veux l'utiliser comme dans le code suivant j'ai un NullPointerException et je ne comprends pas pourquoi car quand je veux faire quelque chose avec le canvas juste au dessus j'ai pas de problème et à ma connaissance normalement je n'ai pas besoin de faire un new dessus. Si quelqu'un sait quelque chose qu'il m'aide svp.

    Dans cette listView je voudrai mettre les noms des fichiers qui sont dans un répertoire nommé "exemples". L'action est lancé grace à un bouton dans une première fenêtre ensuite je charge la deuxième fenêtre simple avec une ListView et un Label qui doit afficher les noms des fichiers.

    Code controleur:
    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
     
    @FXML public Canvas canvas;
    @FXML public ListView<String> listView;
     
    @FXML
    public void rechercheFichier() throws IOException {
    	Stage stage=new Stage();
    	FXMLLoader loader=new FXMLLoader(Main.class.getResource("ChargeFichier.fxml"));
    	Parent root=loader.load();
     
    	ObservableList<String> filesName=FXCollections.observableArrayList();
    	listView.setItems(filesName);
    	File path=new File("exemples");
    	for(File f : path.listFiles()) {
    		filesName.add(f.getName());
    	}
     
    	stage.setTitle("Recherche Fichier");
    	stage.setScene(new Scene(root));
    	stage.show();
    }
    et l'erreur (le null pointer est sur la ligne "listView.setItems(filesName);" ligne 12):
    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
    Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    	at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1787)
    	at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
    	at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    	at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    	at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    	at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    	at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    	at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    	at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    	at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    	at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    	at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    	at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    	at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    	at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    	at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8890)
    	at javafx.controls/javafx.scene.control.Button.fire(Button.java:203)
    	at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
    	at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
    	at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    	at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    	at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    	at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    	at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    	at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    	at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    	at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    	at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    	at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    	at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    	at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    	at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    	at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    	at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3862)
    	at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
    	at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2590)
    	at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409)
    	at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299)
    	at java.base/java.security.AccessController.doPrivileged(Native Method)
    	at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447)
    	at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:412)
    	at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446)
    	at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
    	at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
    	at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    	at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    	at java.base/java.lang.Thread.run(Thread.java:834)
    Caused by: java.lang.reflect.InvocationTargetException
    	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    	at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
    	at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    	at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
    	at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
    	at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1784)
    	... 46 more
    Caused by: java.lang.NullPointerException
    	at projetmode_g6.projetmode_g6/control.ControlBoutons.rechercheFichier(ControlBoutons.java:48)

  2. #2
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 900
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 900
    Billets dans le blog
    54
    Par défaut
    Citation Envoyé par kenyuki Voir le message
    je lui est mis l'id "listView" et quand je veux l'utiliser comme dans le code suivant j'ai un NullPointerException
    • fx:id - le nom du champ annoté @FXML à injecter dans le contrôleur.
    • id - la valeur de la propriété id du contrôle.
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

  3. #3
    Membre averti
    Homme Profil pro
    dev
    Inscrit en
    Octobre 2020
    Messages
    25
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : dev

    Informations forums :
    Inscription : Octobre 2020
    Messages : 25
    Par défaut
    Oui pardon c'est son fx:id
    Code XML : Sélectionner tout - Visualiser dans une fenêtre à part
     <ListView fx:id="list" layoutY="63.0" onMouseClicked="#charge" prefHeight="337.0" prefWidth="292.0" />

  4. #4
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 900
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 900
    Billets dans le blog
    54
    Par défaut
    Dans ce cas est-ce que tu pourrais me montrer comment tu charges ton FXML et son contrôleur, et aussi la ligne 48 de la classe ControlBoutons STP ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    Caused by: java.lang.NullPointerException
    	at projetmode_g6.projetmode_g6/control.ControlBoutons.rechercheFichier(ControlBoutons.java:48)
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

  5. #5
    Membre averti
    Homme Profil pro
    dev
    Inscrit en
    Octobre 2020
    Messages
    25
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : dev

    Informations forums :
    Inscription : Octobre 2020
    Messages : 25
    Par défaut
    Voici:
    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
    @FXML
    public void rechercheFichier() throws IOException {
    	Stage stage=new Stage();
    	FXMLLoader loader=new FXMLLoader(Main.class.getResource("ChargeFichier.fxml"));
    	Parent root=loader.load();
     
    	ObservableList<String> filesName=FXCollections.observableArrayList();
    	list.setItems(filesName);                                            // <- Ligne de l'erreur (ligne 48)
    	File path=new File("exemples");
    	for(File f : path.listFiles()) {
    		filesName.add(f.getName());
    	}
     
    	stage.setTitle("Recherche Fichier");
    	stage.setScene(new Scene(root));
    	stage.show();
    }
    J'ai une première fenêtre dans laquelle je clique sur un bouton et ça me créé une nouvelle fenêtre (celle avec la ListView) les fichiers que je veux mettre dans la ListView sont dans exemples. Je sens que c'est quelque chose de très bête, que j'ai juste oublié quelque chose mais faut pas me taper dessus svp j'apprends x)

    Edit: j'ai changé listView par list pour pas faire de confusion j'avais oublié de préciser

  6. #6
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 900
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 900
    Billets dans le blog
    54
    Par défaut
    1 seconde, qui est le contrôler de ChargeFichier.fxml ?
    Comment as-tu charge l'instance de la classe (qui semble être elle-même un contrôleur puisqu'il y a une annotation @FXML) dans laquelle tu te trouves au moment ou cette exception est levée ?
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

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

Discussions similaires

  1. Réponses: 1
    Dernier message: 12/12/2008, 08h57
  2. [Language]Constructeur==> Null pointer?
    Par Chiwawa dans le forum Langage
    Réponses: 8
    Dernier message: 14/05/2005, 22h51
  3. Faire pointer sur un objet formulaire
    Par bigsister dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 26/11/2004, 16h25
  4. pointer sur une ligne d'un TStringGrid
    Par jeannot27 dans le forum C++Builder
    Réponses: 7
    Dernier message: 20/10/2004, 10h56
  5. Pointer sur un fichier FILE *
    Par Biou Pink Powa dans le forum C
    Réponses: 3
    Dernier message: 29/01/2004, 14h45

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