Bonjour à tous,
je suis débute en JavaFX, mon programme de test à pour but de mettre en place le modèle MVC et je rencontre un problème avec les controleurs.
Quand je clique sur le bouton (CenterControler) j'ai bien un message qui s'affiche sur mon label mais quand je clique sur File->Quit (MainControler) j'ai ce message :
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 Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source) at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source) at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source) at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source) at javafx.event.Event.fireEvent(Unknown Source) at javafx.scene.control.MenuItem.fire(Unknown Source) at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(Unknown Source) at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$324(Unknown Source) at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer$$Lambda$150/11109972.handle(Unknown Source) at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source) at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source) at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source) at javafx.event.Event.fireEvent(Unknown Source) at javafx.scene.Scene$MouseHandler.process(Unknown Source) at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source) at javafx.scene.Scene.impl_processMouseEvent(Unknown Source) at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source) at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source) at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source) at com.sun.glass.ui.View.handleMouseEvent(Unknown Source) at com.sun.glass.ui.View.notifyMouse(Unknown Source) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown Source) at com.sun.glass.ui.win.WinApplication$$Lambda$37/1109371569.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at sun.reflect.misc.Trampoline.invoke(Unknown Source) at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at sun.reflect.misc.MethodUtil.invoke(Unknown Source) ... 44 more Caused by: java.lang.NullPointerException at controler.CenterControler.show(CenterControler.java:42) at controler.MainControler.onClick2(MainControler.java:45) ... 53 more
Voici le code du Main.java
Voici le code de mon MainControler.java
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 package application; import java.io.IOException; import controler.CenterControler; import controler.MainControler; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.stage.Stage; import javafx.scene.Parent; import javafx.scene.Scene; public class Main extends Application { CenterControler ctrl_Center; MainControler ctrl_Main; Stage primaryStage; @Override public void start(Stage primaryStage) throws IOException { FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../view/themain.fxml")); Parent root = (Parent) fxmlLoader.load(); ctrl_Main = (MainControler)fxmlLoader.getController(); Scene scene = new Scene(root, 800, 600); primaryStage.setTitle("FXML Welcome"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
Voici le fichier CenterControler.java
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 package controler; import java.net.URL; import java.util.ResourceBundle; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Menu; import javafx.scene.control.MenuBar; import javafx.scene.control.MenuItem; public class MainControler implements Initializable{ @FXML private Menu File; @FXML private MenuBar barreMenu; @FXML private Menu sousMenu; @FXML private MenuItem menuQuit; @FXML private CenterControler centreControle; @Override public void initialize(URL location, ResourceBundle resources) { assert File != null : "fx:id=\"File\" was not injected: check your FXML file 'themain.fxml'."; assert menuQuit != null : "fx:id=\"menuQuit\" was not injected: check your FXML file 'themain.fxml'."; } public void setCenterControle(CenterControler cc) { this.centreControle = cc; } @FXML protected void onClick2() { centreControle = new CenterControler(); centreControle.show("IT WORKS !!! FROM MAIN CONTROLER"); } }
Le fichier themain.fxml
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 package controler; import java.net.URL; import java.util.ResourceBundle; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Button; import javafx.scene.control.Label; public class CenterControler implements Initializable{ @FXML private Button btn_1; @FXML private Label labelMSG; @Override public void initialize(URL arg0, ResourceBundle arg1) { assert btn_1 != null : "fx:id=\"btn_1\" was not injected: check your FXML file 'part2.fxml'."; assert labelMSG != null : "fx:id=\"labelMSG\" was not injected: check your FXML file 'part2.fxml'."; } public void setBTN(Button btn){ this.btn_1 = btn;} public void setLabel(Label lb) {this.labelMSG = lb; } public Button getBTN(){ return this.btn_1; } public Label getLabel(){ return this.labelMSG;} @FXML protected void onClickShowMSG() { labelMSG.setText("PUSH FROM CENTER CONTROLER"); } public void show(String msg) { labelMSG.setText(msg); } }
Et le fichier part2.fxml
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 <?xml version="1.0" encoding="UTF-8"?> <?import java.lang.*?> <?import java.util.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.paint.*?> <?import javafx.scene.text.*?> <VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controler.MainControler"> <children> <MenuBar fx:id="barreMenu" VBox.vgrow="NEVER"> <menus> <Menu fx:id="sousMenu" mnemonicParsing="false" text="File"> <items> <MenuItem fx:id="menuQuit" mnemonicParsing="false" onAction="#onClick2" text="Quit" /> </items> </Menu> </menus> </MenuBar> <fx:include fx:id="p2" source="part2.fxml" /> </children> </VBox>
Merci d'avance pour votre aide.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.*?> <?import java.lang.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.layout.AnchorPane?> <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controler.CenterControler"> <children> <Button fx:id="btn_1" layoutX="274.0" layoutY="188.0" mnemonicParsing="false" onAction="#onClickShowMSG" text="Button" /> <Label fx:id="labelMSG" layoutX="286.0" layoutY="128.0" text="Label" /> </children> </AnchorPane>![]()
Partager