0

I'm stuck and need some advice. I keep getting NullPointerExceptions while calling getText on my TextField and PasswordField. I'm using fxml files to create the UI, and scene builder as well.

Code :

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();
            }
        }

}
}

The commented code in the controller works fine. I want to put this logic in my model. If i did something wrong please be gentle. I'm a beginner both in coding and 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
------------------------------------------------------------------------
Kazam
  • 11
  • 1
  • What line is throwing this error? Post stacktrace. – SedJ601 May 22 '17 at 13:49
  • Your code is confusing. – SedJ601 May 22 '17 at 13:57
  • What line is line 61 in LoginScreenController? – SedJ601 May 22 '17 at 13:58
  • It appears your local fields are hiding global variables. – SedJ601 May 22 '17 at 14:00
  • Did you actually read the stack trace? Assuming the stack trace came from executing the code you posted, this has nothing to do with your text fields. – James_D May 22 '17 at 14:03
  • In public String getLogin(TextField login_txt) {...} TextField login_txt hides the global variable login_txt and should not be there. You method should start like public String getLogin() {}. Same situation for public String getPassword(PasswordField password_txt){..}. If you are a beginner programmer, you should test every method and verify that it works properly before moving on to the next method. – SedJ601 May 22 '17 at 14:03

0 Answers0