2

I am trying to call a method from another class from my java controller class, but that doesnt seem to work as i get the error when i try to click the loginbutton. Here is what i am trying to do Load FXML>>Enter Login and Password >> Click LOGIN btn>>Call a method frm another class to check credentials The Error is encountered as a Runtime, when I Click the Login Button

SECOND CLASS (frm where i call the method)

package javafxapplication4;

import static globalmethods.GlobalMethods.Display_Text;

public class CheckLogin{

public static final String USERNAME = "admin";//Store the credentials in     a file
public static final String PASSWORD = "admin";//then retrive them here
String username, password;

static void startCheck() {
    CheckLogin obj = new CheckLogin();
    obj.getVars();
    obj.checkLoginCredentials();
    }

    public void getVars() {
        LoginController obj1 = new LoginController();
        username = obj1.getUsername_i();
    password = obj1.getPassword_i();

}

public void checkLoginCredentials() {
    if (username.equals(USERNAME) && password.equals(PASSWORD)) {
        Display_Text("Login Successful!");
    } else {
        Display_Text("Failed to Login. Incorrect Username and/or Password");
    }

}

}

JAVAFX FXML

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

<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.effect.ColorAdjust?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<AnchorPane id="AnchorPane" prefHeight="253.0" prefWidth="417.0" style="-    fx-background-color: #03A9F4;" xmlns="http://javafx.com/javafx/8.0.111"     xmlns:fx="http://javafx.com/fxml/1"     fx:controller="javafxapplication4.LoginController">
   <children>
      <Text fill="WHITE" layoutX="139.0" layoutY="30.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Welcome" textAlignment="CENTER" wrappingWidth="131.6708984375">
         <font>
            <Font size="28.0" />
         </font>
      </Text>
      <JFXTextField fx:id="username_text" accessibleText="username_text" alignment="CENTER" focusColor="WHITE" labelFloat="true" layoutX="120.0"     layoutY="78.0" promptText="Username" unFocusColor="#bdbdbd">
             <effect>
            <ColorAdjust />
     </effect></JFXTextField>
      <JFXTextField fx:id="password_text" accessibleText="password_text" alignment="CENTER" focusColor="WHITE" labelFloat="true" layoutX="121.0" layoutY="127.0" prefHeight="21.0" prefWidth="169.0" promptText="Password" unFocusColor="#bdbdbd" />
  <JFXButton fx:id="login_btn" buttonType="RAISED" layoutX="165.0" layoutY="199.0" onAction="#OnLoginClicked" prefHeight="40.0" prefWidth="87.0" style="-fx-background-color: #FFC107;" text="Login" />
  <Label fx:id="loginresp_lab" alignment="CENTER" layoutX="76.0" layoutY="159.0" prefHeight="21.0" prefWidth="258.0" textAlignment="CENTER" textFill="#d32f2f">
     <font>
        <Font name="System Bold" size="15.0" />
     </font>
  </Label>
  <ImageView fx:id="close_btn" fitHeight="29.0" fitWidth="30.0"     focusTraversable="true" layoutX="388.0" layoutY="4.0"     onMouseClicked="#OnExitClicked" pickOnBounds="true" preserveRatio="true"     smooth="false">
         <image>
        <Image url="@xclose.png" />
     </image>
     <effect>
        <DropShadow blurType="GAUSSIAN" color="#000000af" />
     </effect>
  </ImageView>
   </children>
</AnchorPane>

JAVA CLASS

package javafxapplication4;

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

public class GUITest2 extends Application {

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

    Scene scene = new Scene(root);
    stage.setResizable(false);
    stage.initStyle(StageStyle.UNDECORATED);
    stage.setScene(scene);
    stage.show();
}


public static void main(String[] args) {
    launch(args);
}

}

JAVA CONTROLLER package javafxapplication4;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXTextField;
import static globalmethods.GlobalMethods.Display_Text;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import static javafxapplication4.CheckLogin.startCheck;

public class LoginController implements Initializable {

CheckLogin obj = new CheckLogin();

private String Username_i, Password_i;
@FXML
private JFXTextField username_text;

@FXML
private JFXTextField password_text;

@FXML
private JFXButton login_btn;

@FXML
private Label loginresp_lab;

@FXML
void OnLoginClicked(ActionEvent event) {
    startCheck();

}

public String getUsername_i() {
    return Username_i;
}

public String getPassword_i() {
    return Password_i;
}

@FXML
private ImageView close_btn;

@FXML
void OnExitClicked(MouseEvent event) {
    Display_Text("Exit!");
    System.exit(0);
}

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

}

ERROR

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
   at javafxapplication4.CheckLogin.checkLoginCredentials(CheckLogin.java:25)
        at javafxapplication4.CheckLogin.startCheck(CheckLogin.java:14)
    at    javafxapplication4.LoginController.OnLoginClicked(LoginController.java:52)
    ... 58 more
Rohan Sawant
  • 938
  • 1
  • 11
  • 18

1 Answers1

2

This appears to be a simple NPE (Null pointer exception). I won't parse through all this code, but this line:

username.equals(USERNAME) && password.equals(PASSWORD) 

indicates that either username or password is null. Be conscious of null checks, and how to guard against NPE's.

This is an easy fix to get to the actual problem:

USERNAME.equals(username) && PASSWORD.equals(password)

PASSWORD and USERNAME are constants. These can never be null. After you make this change, you can hunt down exactly why these values are null.

When doing String comparisons, Apache StringUtils.equals is a convenience method that does a null check before a comparison.

Christopher Schneider
  • 3,745
  • 2
  • 24
  • 38