Heyho,
启动javafx项目后,我有一个非常奇怪的加载异常。当我挑起另一个错误时,就会出现loadexception,否则一切似乎都能正常工作。所以我也不理解并且可能与加载异常相关的nullpointerexception是我甚至看到这个loadexception的原因。 我在我的Main类和start()中编写了一个getController()方法,我希望通过loader.getController()来获取控制器,它似乎返回null。在我的@FXML initialize()中,我实例化了模型图,它在构造函数中调用了我的getController()方法,当图trie使用这个构造函数时,我当然得到了一个nullpointerexception .. 有人知道这个问题吗?
这是堆栈跟踪
`javafx.fxml.LoadException:
/C:/Users/bertr/eclipse-workspace/Hashiwokakero/bin/application/view/Layout.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2571)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at application.Main.start(Main.java:30)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
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)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2566)
... 12 more
Caused by: java.lang.NullPointerException
at application.model.Graph.<init>(Graph.java:88)
at application.view.Controller.initialize(Controller.java:68)
... 22 more
`
开始() - 方法
public class Main extends Application {
public static Controller controller;
@Override
public void start(Stage primaryStage) {
try {
//anLeitungLaden();
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("view/Layout.fxml"));
Pane pane = (Pane) loader.load();
controller = loader.getController();
Scene scene = new Scene(pane);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setTitle("Hashiwokakero");
primaryStage.setScene(scene);
//primaryStage.getIcons().add(new Image("resources/logo2.png"));
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
Controller类的开头
package application.view;
import java.io.File;
import java.util.Arrays;
import application.model.Vertex;
import application.Main;
import application.model.Edge;
import application.model.Graph;
import application.model.HashiGenerator;
import application.model.HashiGenerator2;
import javafx.animation.Animation;
import javafx.animation.AnimationTimer;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.SequentialTransition;
import javafx.animation.Timeline;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.geometry.Bounds;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.effect.Glow;
import javafx.scene.input.MouseDragEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.paint.Color;
import javafx.scene.shape.Shape;
import javafx.util.Duration;
public class Controller {
@FXML public Pane root;
@FXML public Pane pane;
@FXML public CheckBox checkboxMusic;
@FXML public CheckBox checkboxSoundeffect;
@FXML public Button newLevelButton;
public MediaPlayer mediaPlayer;
private MediaPlayer soundeffectPlayer;
private boolean soundEffect;
public int numberOfEmptyContainer;
public Graph level;
public DragStartHandler startHandler;
@FXML private void initialize() {
String musicFile = "src/resources/star-wars-cantina-song.mp3";
Media sound = new Media(new File(musicFile).toURI().toString());
mediaPlayer = new MediaPlayer(sound);
mediaPlayer.play();
numberOfEmptyContainer=0;
checkboxMusic.setSelected(true);
checkboxSoundeffect.setSelected(true);
soundEffect = true;
newLevelButton.setVisible(false);
newLevelButton.setDisable(true);
startHandler = new DragStartHandler();
level = new Graph(pane);
和Layout.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.paint.*?>
<?import java.lang.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.text.*?>
<Pane id="root" fx:id="root" prefHeight="839.0" prefWidth="1281.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.view.Controller">
<children>
<Pane id="pane" fx:id="pane" layoutX="21.0" layoutY="64.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="749.0" prefWidth="1209.0" style="-fx-border-radius: 100; -fx-border-width: 100;">
<children>
<Button id="newLevelButton" fx:id="newLevelButton" layoutX="363.0" layoutY="342.0" mnemonicParsing="false" prefHeight="66.0" prefWidth="484.0" style="-fx-border-color: #ffd308; -fx-border-radius: 30;" text="Weiter zu Level 2" textFill="#ffd308">
<font>
<Font name="System Italic" size="40.0" />
</font></Button>
</children></Pane>
<CheckBox id="checkboxSoundeffect" fx:id="checkboxSoundeffect" layoutX="1032.0" layoutY="23.0" mnemonicParsing="false" onAction="#handleCheckboxSoundEffect" text="Soundeffekte" textFill="WHITE" />
<CheckBox id="checkboxMusic" fx:id="checkboxMusic" layoutX="1185.0" layoutY="23.0" mnemonicParsing="false" onAction="#handleCheckboxMusic" text="Musik" textFill="WHITE" />
</children>
</Pane>
编辑: 这是我的类Graph
package application.model;
import java.io.File;
import java.util.Arrays;
import application.Main;
import application.view.Controller;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.scene.Node;
import javafx.scene.input.MouseDragEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
public class Graph {
public int numberOfContainer = 8;
public int[][] adjazenzMatrix;
public int[] nodelist;
public boolean[] nodelistvisited;
private Controller controller;
private Pane pane = new Pane();
public Edge ContainerLine ;
public Vertex startContainer;
// 3 Zeilen/ 4 Spalten
// TODO: Positionen überlegen
public Vertex createContainer(double x, double y, double radius, int anzahl) {
return new Vertex(x, y, radius,anzahl);
}
public Graph(Pane pane){
controller= Main.getController();
pane = pane;
nodelist = new int[numberOfContainer];
adjazenzMatrix = new int[numberOfContainer][numberOfContainer];
for (int i = 0; i < numberOfContainer; ++i ) {
Arrays.fill(adjazenzMatrix[i],0);
nodelist[i] = i;
}
nodelistvisited = new boolean[numberOfContainer] ;
Arrays.fill(nodelistvisited,false);
Vertex[][] containerGrid = new Vertex[][]{
{createContainer(pane.getPrefWidth()-(7*pane.getPrefWidth())/8, (double)pane.getPrefHeight()-(5*pane.getPrefHeight())/6,30,4), null ,createContainer(pane.getPrefWidth()-(7*pane.getPrefWidth())/8, pane.getPrefHeight()-(1*pane.getPrefHeight())/6,30,3)},
{createContainer(pane.getPrefWidth()-(5*pane.getPrefWidth())/8, (double)pane.getPrefHeight()-(5*pane.getPrefHeight())/6,30,5), createContainer(pane.getPrefWidth()-(5*pane.getPrefWidth())/8, (double)pane.getPrefHeight()-(3*pane.getPrefHeight())/6,30,3),null},
{null, createContainer(pane.getPrefWidth()-(3*pane.getPrefWidth())/8, (double)pane.getPrefHeight()-(3*pane.getPrefHeight())/6,30,2),createContainer(pane.getPrefWidth()-(3*pane.getPrefWidth())/8, (double)pane.getPrefHeight()-(1*pane.getPrefHeight())/6,30,1)},
{createContainer(pane.getPrefWidth()-(1*pane.getPrefWidth())/8, (double)pane.getPrefHeight()-(5*pane.getPrefHeight())/6,30,1), createContainer(pane.getPrefWidth()-(1*pane.getPrefWidth())/8, (double)pane.getPrefHeight()-(3*pane.getPrefHeight())/6,30,1),null}
};
//Zeilen X Richtung
for (int i = 0; i < 4; ++i) {
//Spalten Y Richtung
for ( int j = 0; j<3; ++j) {
if(containerGrid[i][j] != null) {
Vertex c = containerGrid[i][j];
c.setOnDragDetected(controller.startHandler);
c.setOnMouseDragReleased(controller.dragReleaseHandler);
c.setOnMouseDragEntered(controller.dragEnteredHandler);
c.setUserData(Boolean.TRUE);
c.getChildren().addAll(c.circle, c.label,c.label2);
pane.getChildren().add(c);
}
}
}
} // KostruktorLevel
public void deepSearch(int[][] matrix,int[] nodelist ,boolean[] nodelistvisited ,int current,int suche){
nodelistvisited[current]=true; // Knoten als besucht kennzeichnen
for (int i=0;i<nodelist.length;i++)
{
if(matrix[current][i] == 1&& !nodelistvisited[i]) deepSearch(matrix,nodelist,nodelistvisited,i,suche);
}
}
}