使用TextField getText()时java.lang.NullPointerException。我在TextField中是否有文字都没关系

时间:2020-11-07 17:21:23

标签: java javafx nullpointerexception textfield gettext

场景似乎已正确加载。当我尝试调用将getText()方法用于TextField的handle方法时,我一直在获取java.lang.NullPointerException。我以前曾用过这个,没有问题,但是我想我缺少一些东西。

错误:

线程“ JavaFX Application Thread”中的异常java.lang.RuntimeException:> java.lang.reflect.InvocationTargetException 在javafx.fxml.FXMLLoader $ MethodHandler.invoke(FXMLLoader.java:1774) 在javafx.fxml.FXMLLoader $ ControllerMethodEventHandler.handle(FXMLLoader.java:1657) 在com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) 在com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 在com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 在> com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) 在com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 在com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 在com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 在com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 在com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) 在javafx.event.Event.fireEvent(Event.java:198) 在javafx.scene.Scene $ KeyHandler.process(Scene.java:3964) 在javafx.scene.Scene $ KeyHandler.access $ 1800(Scene.java:3910) 在javafx.scene.Scene.impl_processKeyEvent(Scene.java:2040) 在javafx.scene.Scene $ ScenePeerListener.keyEvent(Scene.java:2501) 在> com.sun.javafx.tk.quantum.GlassViewEventHandler $ KeyEventNotification.run(GlassViewEventHandler.java:217) 在> com.sun.javafx.tk.quantum.GlassViewEventHandler $ KeyEventNotification.run(GlassViewEventHandler.java:149) 在java.security.AccessController.doPrivileged(本机方法) 在> com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda $ handleKeyEvent $ 1(GlassViewEventHandler.java:248) 在com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:410) 在com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:247) 在com.sun.glass.ui.View.handleKeyEvent(View.java:546) 在com.sun.glass.ui.View.notifyKey(View.java:966) 在com.sun.glass.ui.win.WinApplication._runLoop(本机方法) 在com.sun.glass.ui.win.WinApplication.lambda $ null $ 4(WinApplication.java:186) 在java.lang.Thread.run(未知来源) 造成原因:java.lang.reflect.InvocationTargetException 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 在sun.reflect.NativeMethodAccessorImpl.invoke(未知来源) 在sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源) 在java.lang.reflect.Method.invoke(未知来源) 在sun.reflect.misc.Trampoline.invoke(未知来源) 在sun.reflect.GeneratedMethodAccessor1.invoke(未知来源) 在sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源) 在java.lang.reflect.Method.invoke(未知来源) 在sun.reflect.misc.MethodUtil.invoke(未知来源) 在javafx.fxml.FXMLLoader $ MethodHandler.invoke(FXMLLoader.java:1769) ...另外29个 引起原因:java.lang.RuntimeException:java.lang.reflect.InvocationTargetException 在javafx.fxml.FXMLLoader $ MethodHandler.invoke(FXMLLoader.java:1774) 在javafx.fxml.FXMLLoader $ ControllerMethodEventHandler.handle(FXMLLoader.java:1657) 在com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) 在com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 在com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 在> com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) 在com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 在com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 在com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 在com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 在com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49) 在javafx.event.Event.fireEvent(Event.java:198) 在javafx.scene.Node.fireEvent(Node.java:8411) 在javafx.scene.control.Button.fire(Button.java:185) 在application.controller.LoginController.keyPressed(LoginController.java:40) ...另外39个 造成原因:java.lang.reflect.InvocationTargetException 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 在sun.reflect.NativeMethodAccessorImpl.invoke(未知来源) 在sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源) 在java.lang.reflect.Method.invoke(未知来源) 在sun.reflect.misc.Trampoline.invoke(未知来源) 在sun.reflect.GeneratedMethodAccessor1.invoke(未知来源) 在sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源) 在java.lang.reflect.Method.invoke(未知来源) 在sun.reflect.misc.MethodUtil.invoke(未知来源) 在javafx.fxml.FXMLLoader $ MethodHandler.invoke(FXMLLoader.java:1769) ...还有56个 造成原因:java.lang.NullPointerException 在application.controller.LoginController.handle(LoginController.java:57) ...另外66个

Main.java

package application;
    
import application.controller.LoginController;
import application.model.Fleet;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;

public class Main extends Application {
    
    
    public void start(Stage primaryStage) {
        
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/Login.fxml"));
            Parent firstPane = loader.load();
            Scene firstScene = new Scene(firstPane);
            
            LoginController scene1Controller = loader.getController();
            scene1Controller.f = new Fleet();
            
            primaryStage.setTitle("Starfleet Personnel");
            primaryStage.getIcons().add(new Image("file:startfleet-icon.png"));
            primaryStage.setScene(firstScene);
            primaryStage.show();
            
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}

FXML

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

<?import javafx.scene.paint.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane maxHeight="800.0" maxWidth="800.0" 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="application.controller.LoginController">
   <top>
      <HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0" spacing="30.0" BorderPane.alignment="CENTER">
         <children>
            <ImageView id="logo" fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@starfleet.jpg" />
               </image>
               <HBox.margin>
                  <Insets left="15.0" top="15.0" />
               </HBox.margin>
            </ImageView>
            <Text id="textTitle" fill="BLUE" strokeType="OUTSIDE" strokeWidth="0.0" text="Starfleet Personnel" textAlignment="CENTER" underline="true">
               <HBox.margin>
                  <Insets />
               </HBox.margin>
               <font>
                  <Font name="Lucida Bright Demibold" size="36.0" />
               </font>
            </Text>
         </children>
         <BorderPane.margin>
            <Insets />
         </BorderPane.margin>
         <padding>
            <Insets bottom="15.0" />
         </padding>
      </HBox>
   </top>
   <padding>
      <Insets bottom="15.0" left="15.0" right="15.0" top="15.0" />
   </padding>
   <center>
      <VBox alignment="CENTER" spacing="15.0" BorderPane.alignment="CENTER">
         <children>
            <HBox alignment="CENTER" spacing="15.0">
               <children>
                  <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Enter Your Username:" underline="true">
                     <font>
                        <Font name="Lucida Bright Demibold" size="24.0" />
                     </font>
                  </Text>
                  <TextField id="inputUsername" onKeyPressed="#keyPressed" prefWidth="200.0" promptText="Enter User Name" />
               </children>
               <padding>
                  <Insets left="75.0" />
               </padding>
            </HBox>
            <HBox alignment="CENTER" spacing="15.0">
               <children>
                  <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Enter Passphrase:" underline="true">
                     <font>
                        <Font name="Lucida Bright Demibold" size="24.0" />
                     </font>
                  </Text>
                  <PasswordField id="inputPass" onKeyPressed="#keyPressed" promptText="Enter Passphrase" />
               </children>
               <padding>
                  <Insets left="75.0" />
               </padding>
            </HBox>
            <Text id="errorText" fill="RED" strokeType="OUTSIDE" strokeWidth="0.0">
               <font>
                  <Font name="Lucida Bright Demibold" size="18.0" />
               </font>
            </Text>
            <Button fx:id="loginButton" mnemonicParsing="false" onAction="#handle" text="Login" textFill="BLUE">
               <font>
                  <Font name="Lucida Bright Demibold" size="18.0" />
               </font>
            </Button>
         </children>
         <padding>
            <Insets top="15.0" />
         </padding>
      </VBox>
   </center>
</BorderPane>

LoginController

package application.controller;

import java.io.IOException;

import application.model.Fleet;
import application.model.Starship;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class LoginController{
    
    public Fleet f;
    
    @FXML 
    public TextField inputUsername;
    public PasswordField inputPass;
    public Text errorText;
    public Button loginButton;

    @FXML
    private void keyPressed(KeyEvent keyEvent){
        if (keyEvent.getCode() == KeyCode.ENTER) {
            loginButton.fire();
        }
    }

    @FXML
    public void handle(ActionEvent event){
        Starship check;
        if(inputUsername.getText()==null || inputUsername.getText().isEmpty()) {
            errorText.setText("No Username entered. Please enter a Username.");
        }
        else if(inputPass.getText()==null || inputPass.getText().isEmpty()) {
            errorText.setText("No Passphrase entered. Please enter a Passphrase.");
        }
        else{
            check = f.validate(inputUsername.getText(), inputPass.getText());
            if(check != null) {
                this.loadSceneAndSendData(check);
            }
            else {
                errorText.setText("Username or Passphrase incorrect. Please try again.");
            }
        }
    }
    
    private void loadSceneAndSendData(Starship s) {
        try {
            //Load second scene
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/Personnel.fxml"));
            Parent root = loader.load();
             
            //Get controller of scene2
            PersonnelController scene2Controller = loader.getController();
            scene2Controller.setFleet(this.f);
            scene2Controller.setStarshipView(s);
            scene2Controller.ShowCrew();
 
            //Show scene 2 in new window            
            Stage stage = new Stage();
            stage.setScene(new Scene(root));
            stage.setTitle("Second Window");
            stage.show();
        } catch (IOException ex) {
            System.err.println(ex);
        }
    }
}

0 个答案:

没有答案
相关问题