当我单击主页的开始按钮时,我正在主页中间加载 SimulatorWindow.fxml 文件。
在给定图像的红色标记区域中,我需要加载另一个 fxml 文件(名为 Simulator.fxml)。
这是我的 SimulatorWindowController.java 文件
package SortingAlgorithmSimulator.view;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import SortingAlgorithmSimulator.Main;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class SimulatorWindowController extends Application
{
private HomeController hc=new HomeController();
@FXML
private TextArea AlgorithmField;
@FXML
private CheckBox screenSize;
@FXML
public static BorderPane simu;
@FXML
private void initialize() throws IOException
{ FXMLLoader loader1=new FXMLLoader();
loader1.setLocation(Main.class.getResource("view/Simulator.fxml"));
BorderPane mainItems1=loader1.load();
simu.setCenter(mainItems1);
BufferedReader buff = null;
try {
buff = new BufferedReader(new FileReader(hc.selectedFile));
String str;
while ((str = buff.readLine()) != null)
{
AlgorithmField.appendText(str+"\n");
}
} catch (IOException e)
{
} finally
{
try { buff.close(); } catch (Exception ex) { }
}
}
@FXML
private void goHome() throws IOException
{
Main.showHome();
}
@FXML
private void goHelp() throws IOException
{
Main.showHelp();
}
@FXML
private void goAboutMe() throws IOException
{
Main.showAboutMe();
}
@FXML
private void goScreen() throws IOException
{
if(screenSize.isSelected())
{
Main.primaryStage.setFullScreen(true);
screenSize.setText("Exit Full Screen");
}
else
{
Main.primaryStage.setFullScreen(false);
screenSize.setText("Full Screen");
}
}
@FXML
private void goExit() throws IOException
{
System.exit(0);
}
@Override
public void start(Stage arg0) throws Exception
{
// TODO Auto-generated method stub
}
}
但是当我运行这段代码时,它显示错误 NullPointerException。
这是错误报告
线程“JavaFX 应用程序线程”中的异常 java.lang.RuntimeException: javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source) at java.lang.reflect.InvocationTargetException 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.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.EventUtil.fireEventImpl(Unknown Source) at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source) at javafx.event.Event.fireEvent( Unknown Source) at javafx.scene.Node.fireEvent(Unknown Source) at javafx.scene.control.Button.fire(Unknown Source) at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source) at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source) at com.sun.javafx.scene.control.skin。com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source) com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source) com.sun.javafx.event 的 BehaviorSkinBase$1.handle(Unknown Source) .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.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(未知来源)在 com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification。在 com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(Unknown Source) at com.sun.javafx.tk.quantum.QuantumToolkit 在 java.security.AccessController.doPrivileged(Native Method) 运行(Unknown Source) .runWithoutRenderLock(Unknown Source) 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$148(Unknown Source) at java.lang。 Thread.run(Unknown Source) 原因:sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 处 sun.reflect 处的 java.lang.reflect.InvocationTargetException。Sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 在 sun.reflect.misc.Trampoline.invoke(Unknown Source) 在 sun.java.lang.reflect.Method.invoke(Unknown Source) 的 NativeMethodAccessorImpl.invoke(Unknown Source)。 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)。 .. 49 更多原因:javafx.fxml.LoadException: /D:/Document%20Files/Java%20Programming/bin/SortingAlgorithmSimulator/view/SimulatorWindow.fxml在 sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 在 sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source) 在 sun.reflect.Method.invoke(Unknown Source) 在 sun.reflect.misc.invoke(Unknown Source) 在 sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)。 MethodUtil.invoke(Unknown Source) ... 49 更多原因:javafx.fxml.LoadException: /D:/Document%20Files/Java%20Programming/bin/SortingAlgorithmSimulator/view/SimulatorWindow.fxml在 sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 在 sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source) 在 sun.reflect.Method.invoke(Unknown Source) 在 sun.reflect.misc.invoke(Unknown Source) 在 sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)。 MethodUtil.invoke(Unknown Source) ... 49 更多原因:javafx.fxml.LoadException: /D:/Document%20Files/Java%20Programming/bin/SortingAlgorithmSimulator/view/SimulatorWindow.fxml/Document%20Files/Java%20Programming/bin/SortingAlgorithmSimulator/view/SimulatorWindow.fxml/Document%20Files/Java%20Programming/bin/SortingAlgorithmSimulator/view/SimulatorWindow.fxml
在 javafx.fxml.FXMLLoader.constructLoadException(Unknown Source) at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) at javafx.fxml.FXMLLoader.load(Unknown Source) at SortingAlgorithmSimulator .Main.showSimulatorWindow(Main.java:87) at SortingAlgorithmSimulator.view.HomeController.goSimulatorWindow(HomeController.java:64) ... 58 更多原因:java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0( Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 在 java.lang.reflect.Method.invoke(Unknown Source) 在 sun.reflect.misc.Trampoline。在 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) ... 63 更多原因:SortingAlgorithmSimulator.view.SimulatorWindowController.initialize(SimulatorWindowController.java:35) 处的 java.lang.NullPointerException ... 72 更多
我该如何解决?
这里是源代码的下载链接
https://drive.google.com/folderview?id=0B7pxly3GctSNQ1FaajY4LTFOQnM&usp=sharing