2017-03-01 62 views
2

我试图让我的JavaFX程序在按下按钮时切换屏幕,但我遇到了这个问题。我收到一串错误消息,其中一个是'java.lang.NullPointerException:位置是必需的。'我所有的程序文件都保存在同一个软件包中。java.lang.NullPointerException:位置是必需的。 Netbeans 8.2

主要方法:

package therealcompsciia; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 

public class TheRealCompSciIA extends Application { 

    @Override 
    public void start(Stage stage) throws Exception { 
     Parent root = FXMLLoader.load(getClass().getResource("NewSoccerAppInitialScreen.fxml")); 

     Scene scene = new Scene(root); 

     stage.setScene(scene); 
     stage.show(); 
    } 
/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 
    launch(args); 
} 

} 

这里是初始屏幕FXML代码,NewSoccerAppInitialScreen.fxml

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.Label?> 
<?import javafx.scene.image.Image?> 
<?import javafx.scene.image.ImageView?> 
<?import javafx.scene.layout.AnchorPane?> 
<?import javafx.scene.text.Font?> 

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="336.0" prefWidth="456.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="NewSoccerAppInitialScreenController"> 
    <children> 
    <Button fx:id="PlayerProfiles" alignment="CENTER" layoutX="15.0" layoutY="148.0" mnemonicParsing="false" onAction="#playerprofilesButton" prefHeight="90.0" prefWidth="134.0" text="Player Profiles" textFill="#bf1616"> 
    <font> 
     <Font size="14.0" /> 
    </font></Button> 
    <Button fx:id="Statistics" alignment="CENTER" layoutX="162.0" layoutY="148.0" mnemonicParsing="false" onAction="#statisticsButton" prefHeight="90.0" prefWidth="134.0" text="Statistics" textFill="#bf1616"> 
    <font> 
     <Font size="14.0" /> 
    </font> 
    </Button> 
    <Button fx:id="FormationEditor" alignment="CENTER" layoutX="308.0" layoutY="148.0" mnemonicParsing="false" onAction="#formationeditorButton" prefHeight="90.0" prefWidth="134.0" text="Formation Editor" textFill="#bf1616"> 
    <font> 
     <Font size="14.0" /> 
    </font> 
    </Button> 
    <ImageView fitHeight="98.0" fitWidth="198.0" layoutY="238.0" pickOnBounds="true" preserveRatio="true"> 
    <image> 
     <Image url="@CompSci%20IA%20Grass.jpe" /> 
    </image> 
    </ImageView> 
    <ImageView fitHeight="98.0" fitWidth="198.0" layoutX="135.0" layoutY="238.0" pickOnBounds="true" preserveRatio="true"> 
    <image> 
     <Image url="@CompSci%20IA%20Grass.jpe" /> 
    </image> 
    </ImageView> 
    <ImageView fitHeight="98.0" fitWidth="198.0" layoutX="255.0" layoutY="238.0" pickOnBounds="true" preserveRatio="true"> 
    <image> 
     <Image url="@CompSci%20IA%20Grass.jpe" /> 
    </image> 
    </ImageView> 
    <ImageView fitHeight="98.0" fitWidth="198.0" layoutX="316.0" layoutY="238.0" pickOnBounds="true" preserveRatio="true"> 
    <image> 
     <Image url="@CompSci%20IA%20Grass.jpe" /> 
    </image> 
    </ImageView> 
    <ImageView fitHeight="78.0" fitWidth="107.0" layoutX="296.0" layoutY="76.0" pickOnBounds="true" preserveRatio="true"> 
    <image> 
     <Image url="@CompSci%20IA%20Goal.jpe" /> 
    </image> 
    </ImageView> 
    <ImageView fitHeight="98.0" fitWidth="64.0" layoutX="63.0" layoutY="73.0" pickOnBounds="true" preserveRatio="true"> 
    <image> 
     <Image url="@CompSci%20IA%20Soccer%20Ball.jpe" /> 
    </image> 
    </ImageView> 
    <Label layoutX="114.0" layoutY="30.0" text="DM Soccer Manager" textFill="#bf1616"> 
    <font> 
     <Font name="System Bold" size="24.0" /> 
    </font> 
    </Label> 
    </children> 
    </AnchorPane> 

这里是我的初始屏控制器的代码,NewSoccerAppInitialScreenController.java

import java.io.IOException; 
import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader; 
import javafx.fxml.Initializable; 
import javafx.scene.Node; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.stage.Stage; 

public class NewSoccerAppInitialScreenController implements Initializable { 

@FXML 
private Button PlayerProfiles; 
@FXML 
private Button Statistics; 
@FXML 
private Button FormationEditor; 

    @FXML 
    private void formationeditorButton(ActionEvent event) throws IOException{ 
    Parent FXMLDocument2Parent = FXMLLoader.load(getClass().getClassLoader().getResource("fxml_NewFormationsInitialScreen.fxml")); 
    Scene FXMLDocument2Scene = new Scene(FXMLDocument2Parent); 
    Stage AppStage = (Stage) ((Node) event.getSource()).getScene().getWindow(); 
    //AppStage.hide(); 
    AppStage.setScene(FXMLDocument2Scene); 
    AppStage.show(); 
    } 

    @FXML 
    private void playerprofilesButton(ActionEvent event) throws IOException{ 
    Parent FXMLDocument2Parent = FXMLLoader.load(getClass().getClassLoader().getResource("fxml_NewPlayerProfilesInitial.fxml")); 
    Scene FXMLDocument2Scene = new Scene(FXMLDocument2Parent); 
    Stage AppStage = (Stage) ((Node) event.getSource()).getScene().getWindow(); 
    //AppStage.hide(); 
    AppStage.setScene(FXMLDocument2Scene); 
    AppStage.show(); 
    } 

    @FXML 
    private void statisticsButton(ActionEvent event) throws IOException{ 
    Parent FXMLDocument2Parent = FXMLLoader.load(getClass().getClassLoader().getResource("fxml_NewStatisticsInitial.fxml")); 
    Scene FXMLDocument2Scene = new Scene(FXMLDocument2Parent); 
    Stage AppStage = (Stage) ((Node) event.getSource()).getScene().getWindow(); 
    //AppStage.hide(); 
    AppStage.setScene(FXMLDocument2Scene); 
    AppStage.show(); 
    } 


@Override 
public void initialize(URL url, ResourceBundle rb) { 
    // TODO 
}  

} 

以下是错误消息:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException 
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774) 
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657) 
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49) 
at javafx.event.Event.fireEvent(Event.java:198) 
at javafx.scene.Node.fireEvent(Node.java:8413) 
at javafx.scene.control.Button.fire(Button.java:185) 
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182) 
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96) 
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89) 
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218) 
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) 
at javafx.event.Event.fireEvent(Event.java:198) 
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757) 
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485) 
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762) 
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494) 
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381) 
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295) 
at java.security.AccessController.doPrivileged(Native Method) 
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417) 
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389) 
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416) 
at com.sun.glass.ui.View.handleMouseEvent(View.java:555) 
at com.sun.glass.ui.View.notifyMouse(View.java:937) 
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) 
at java.lang.Thread.run(Thread.java:745) 
Caused by: java.lang.reflect.InvocationTargetException 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71) 
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275) 
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769) 
... 48 more 
Caused by: java.lang.NullPointerException: Location is required. 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104) 
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097) 
at NewSoccerAppInitialScreenController.formationeditorButton(NewSoccerAppInitialScreenController.java:37) 
... 58 more 

我不确定是什么问题。文件名是正确的,并且都存在于同一个项目下的相同包中。我的初始屏幕加载并且可见,但只要点击其中一个按钮将我发送到新屏幕,就会显示上面的错误消息。

+0

的问题是,'的getClass()getClassLoader()的getResource( “fxml_NewPlayerProfilesInitial.fxml”)'因为找不到资源而返回null。也许[这](http://stackoverflow.com/a/28266774/5115768)可能会帮助你? – lenny

回答

相关问题