从我的模型

时间:2017-05-22 13:45:49

标签: javafx nullpointerexception

我被困住了,需要一些建议。我在TextField和PasswordField上调用getText时不断获取NullPointerExceptions。我也使用fxml文件来创建UI和场景构建器。

代码:

public class LoginScreenController {

    private Model model;

    private final String userName = "Kazam";
    private final String userPassword = "Kazam420";
    @FXML
    TextField login_txt;
    @FXML
    PasswordField password_txt;

    @FXML
    private MainScreenController mainscreencontroller;

    @FXML
    Pane loginScreen;

    @FXML
    public void Login() {
       /* FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/Options.fxml"));
        Pane pane = null;
        String name = login_txt.getText();
        String password = password_txt.getText();
        if (name.equals(userName) && password.equals(userPassword)) {

            try {
                pane = loader.load();

            } catch (IOException ex) {
                ex.printStackTrace();
            }
            OptionsScreenController optionsScreenController = loader.getController();
            optionsScreenController.setMainScreenController(mainscreencontroller);
            mainscreencontroller.setScreen(pane);
        }*/
       model.login();

    }
    public void setScreen(Pane pane){
    loginScreen.getChildren().clear();
    loginScreen.getChildren().add(pane);
    }


    public void setMainScreenController(MainScreenController mainscreencontroller) {
        this.mainscreencontroller = mainscreencontroller;
    }



    public String getPassword(PasswordField password_txt){
    String password = password_txt.getText();
    return password;
    }

    public String getLogin(TextField login_txt) {
    String login = login_txt.getText();
    return login;    
    }


    }





public class Model {

private LoginScreenController loginScreenController;


private final String userName = "Kazam";
private final String userPassword = "Kazam420";

 @FXML
 private MainScreenController mainscreencontroller;
 @FXML   
 private TextField login_txt;
 @FXML   
 private PasswordField password_txt;


String name = loginScreenController.getLogin(login_txt);
String password = loginScreenController.getPassword(password_txt);


    public void login(){
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/Options.fxml"));
        Pane pane;

        if (name.equals(userName) && password.equals(userPassword) ){
            try {
                pane = loader.load();
                mainscreencontroller.setScreen(pane);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

}
}

控制器中的注释代码工作正常。我想把这个逻辑放在我的模型中。如果我做错了,请保持温和。我是编码和stackoverflow的初学者。 StackTrace:

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)
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:1771)
    ... 45 more
Caused by: java.lang.NullPointerException
    at Controllers.LoginScreenController.Login(LoginScreenController.java:61)
    ... 55 more
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 15.783s
Finished at: Mon May 22 15:36:21 CEST 2017
Final Memory: 20M/182M
------------------------------------------------------------------------

0 个答案:

没有答案