“应用程序启动方法java.lang.reflect.InvocationTargetException中的异常”

时间:2019-01-20 00:50:41

标签: java javafx scenebuilder

我无法加载启动方法,因此应用程序将无法启动。

因此,我有一个名为StAnnesApp的主类,该主类运行代码并具有start方法。还创建了一个主控制器类来控制我如何在所有场景之间切换。

我正在为此项目使用Scene Builder,所以我有一个LoginController类,应在其中加载应用程序并作为第一页打开,并在其中获取错误。

这是StAnnesApp.java

package st.annesapp;


import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import Controllers.LoginMainController;
import Controllers.MainMenuController;


public class StAnnesApp extends Application {


private static MainController  mainScreen;


public static MainController getMenu()
{
    return mainScreen;
}

private Group root;

@Override
public void start(Stage primaryStage) {


try {

    Group group = new Group();
    mainScreen = new MainController(group);


    Scene scene = new Scene(group);
    mainScreen.startApp();
    primaryStage.setResizable(false);
    primaryStage.setScene(scene);
    primaryStage.show();

    } catch (Exception e) {
      e.printStackTrace();
    }


}




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



}

这是MainController.java(之所以称为Main controller,是因为它正在控制其他控制器类)

package st.annesapp;




public class MainController extends Parent
{

    private final Group root;

    private LoginMainController loginMain;
    private MainMenuController mainMenu;



    public MainController (Group _group)

    {
        this.root = _group;


    }

    public void startApp()
{

    loginMain = new LoginMainController();
    root.getChildren().addAll(loginMain);


}




   public void changeScreen (int screenNumber)
   {

     switch (screenNumber)
      {
          case 1:
          root.getChildren().remove(loginMain);
          loginMain = null;
          mainMenu = new MainMenuController();
          root.getChildren().add(mainMenu);
          break;

          case 2:
              root.getChildren().remove(mainMenu);
              mainMenu = null;


      }



   }


}

这是LoginMainController.java(这是我收到错误的地方)

package Controllers;

import java.io.File;
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.PasswordField;
import javafx.scene.control.TextField;
import java.io.IOException;
import static java.rmi.server.RMIClassLoader.getClassLoader;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import st.annesapp.StAnnesApp;


public class LoginMainController extends Parent implements Initializable {



@FXML
private TextField lblUser;

@FXML
private PasswordField lblPass;

@FXML
private Button btnLogin;












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




}    

public LoginMainController()
{


    addNodesToLayout();
    logIn();

}


@FXML
public void buttonAction(ActionEvent e)
{

     if (lblUser.getText().equals("dorset"))
     {
         StAnnesApp.getMenu().changeScreen(1);

     }


}




private void logIn ()
{

     lblPass = new PasswordField();
     lblUser = new TextField();
     btnLogin = new Button();

}



private void addNodesToLayout ()
{

    try {  



 Parent root = XMLLoader.load(getClass().getResource("/FXML/LoginMain.fxml"));
        getChildren().add(root);

    } catch (IOException ex) {

Logger.getLogger(LoginMainController.class.getName()).log(Level.SEVERE, null, ex);
    }



}
}

这是我从中得到的错误。

ant -f "C:\\Users\\paulo\\Documents\\Multimedia Application\\St.AnnesApp" jfxsa-run
init:
Deleting: C:\Users\paulo\Documents\Multimedia Application\St.AnnesApp\build\built-jar.properties
deps-jar:
Updating property file: C:\Users\paulo\Documents\Multimedia Application\St.AnnesApp\build\built-jar.properties
Compiling 1 source file to C:\Users\paulo\Documents\Multimedia Application\St.AnnesApp\build\classes
compile:
Deleting directory C:\Users\paulo\Documents\Multimedia Application\St.AnnesApp\dist\lib
Copying 2 files to C:\Users\paulo\Documents\Multimedia Application\St.AnnesApp\dist\lib
Detected JavaFX Ant API version 1.3
Launching <fx:jar> task from C:\Program Files\Java\jdk1.8.0_131\jre\..\lib\ant-javafx.jar
Warning: From JDK7u25 the Codebase manifest attribute should be used to restrict JAR repurposing.
         Please set manifest.custom.codebase property to override the current default non-secure value '*'.
Launching <fx:deploy> task from C:\Program Files\Java\jdk1.8.0_131\jre\..\lib\ant-javafx.jar
No base JDK. Package will use system JRE.
No base JDK. Package will use system JRE.
jfx-deployment-script:
jfx-deployment:
jar:
Copying 14 files to C:\Users\paulo\Documents\Multimedia Application\St.AnnesApp\dist\run1928276430
jfx-project-run:
Executing C:\Users\paulo\Documents\Multimedia Application\St.AnnesApp\dist\run1928276430\St.AnnesApp.jar using platform C:\Program Files\Java\jdk1.8.0_131\jre/bin/java
Exception in Application start method
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 com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    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.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.StackOverflowError
    at java.util.concurrent.ConcurrentHashMap.putIfAbsent(ConcurrentHashMap.java:1535)
    at java.lang.ClassLoader.getClassLoadingLock(ClassLoader.java:463)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:404)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at sun.reflect.GeneratedConstructorAccessor2.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at java.lang.Class.newInstance(Class.java:442)
    at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
    at 
   javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
    at 
   javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    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 





 Controllers.LoginMainController.addNodesToLayout(LoginMainController.java:110)
    at Controllers.LoginMainController.<init>(LoginMainController.java:68)

这是LoginMain.fxml类

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

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

<AnchorPane id="AnchorPane" fx:id="root" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: #2D3447;" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" >
   <children>
      <TextField fx:id="lblUser" layoutX="192.0" layoutY="180.0" prefHeight="26.0" prefWidth="220.0" promptText="Username" />
      <PasswordField fx:id="lblPass" layoutX="190.0" layoutY="229.0" prefHeight="26.0" prefWidth="220.0" promptText="Password" />
      <Button fx:id="btnLogin" layoutX="241.0" layoutY="288.0" mnemonicParsing="false" onAction="#buttonAction" prefHeight="32.0" prefWidth="123.0" text="Login" />
      <ImageView fitHeight="112.0" fitWidth="157.0" layoutX="244.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@../Images/woods-marker.png" />
         </image>
      </ImageView>
      <Label layoutX="227.0" layoutY="144.0" prefHeight="27.0" prefWidth="183.0" text="St.Annes Park App" textFill="#fff2f2">
         <font>
            <Font size="18.0" />
         </font>
      </Label>
   </children>
</AnchorPane>

0 个答案:

没有答案