0

I was trying to change the text of a label, and this line :

alertLabel.setText("The credentials are Wrong. Please try again. If you don't already have an account. Please sign up, first");

is giving me error. I have tried commenting this line, and the application works fine.

Here, is my controller class :

public class Controller {
public TextField email_login;
public TextField password_login;
public TextField name_signup;
public TextField email_signup;
public TextField password_signup;
public TextField cpass_signup;
@FXML private Label alertLabel;
public loginModel lm = new loginModel();

public void loginButtonCliked(ActionEvent event) throws IOException{
    if(lm.loginUser(email_login.getText(), password_login.getText())) {
        Parent mainPage = FXMLLoader.load(getClass().getResource("sample1.fxml"));
        Stage s = (Stage) (((Node) event.getSource()).getScene().getWindow());
        s.setScene(new Scene(mainPage));
        s.show();
    }
    else{
        System.out.println("login failed");
        Parent alertPage = FXMLLoader.load(getClass().getResource("alertBox.fxml"));
        Stage st = new Stage();
        st.setScene(new Scene(alertPage));
        alertLabel.setText("The credentials are Wrong. Please try again. If you don't already have an account. Please sign up, first");
        st.show();
    }
}
public void signupButtonClicked(ActionEvent event) throws IOException{
    String n = name_signup.getText();
    String e = email_signup.getText();
    String p = password_signup.getText();
    String c = cpass_signup.getText();
    if(c.trim().equals(p.trim())) {
        lm.insertUser(n, e, p);
        Parent mainPage = FXMLLoader.load(getClass().getResource("sample1.fxml"));
        Stage s = (Stage)(((Node)event.getSource()).getScene().getWindow());
        s.setScene(new Scene(mainPage));
        s.show();
    }
    else{
        System.out.println("There is an error. Please try again...");
    }
}
public void okButtonCliked(ActionEvent event){
    Stage s = (Stage) ((Node)event.getSource()).getScene().getWindow();
    s.close();
}

}

And here is the error that I got :

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:8411)
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:380)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
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:1769)
... 45 more
Caused by: java.lang.NullPointerException
at sample.Controller.loginButtonCliked(Controller.java:42)
... 55 more

Here is my fxml file that contains the alertLabel :

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

<?import javafx.scene.control.*?>


<?import java.lang.*?>
<?import javafx.scene.layout.*?>


<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="175.0" prefWidth="334.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <Label fx:id="alertLabel" layoutX="25.0" layoutY="20.0" prefHeight="85.0" prefWidth="293.0" text="There is an error" />
      <Button layoutX="132.0" layoutY="119.0" mnemonicParsing="false" onAction="#okButtonCliked" prefHeight="42.0" prefWidth="70.0" text="OK" />
   </children>
</AnchorPane>

Any help will be appreciated. I am really new to JavaFX. Thank You!

Ronit
  • 127
  • 1
  • 8
  • possible duplicate of https://stackoverflow.com/questions/23132302/invocationtargetexception-when-running-a-javafx-program – Lokesh Pandey Oct 26 '17 at 06:37
  • I would focus on this Caused by: java.lang.NullPointerException at sample.Controller.loginButtonCliked(Controller.java:42) looks like you have a nullpointer in your code and this causes the rest of exceptions. – MissingSemiColon Oct 26 '17 at 06:39
  • @MissingSemiColon inside loginButtonClicked, in 42 line, this is the code, alertLabel.setText(), as I have mentioned in the question – Ronit Oct 26 '17 at 07:10
  • @Lokesh I don't think, they are the same problems – Ronit Oct 26 '17 at 07:14
  • Maybe but you should start by solving the Nullpointer first. JavaFX is calling your function, if it throws an uncontrolled NullPointerException this error is propagated to the JavaFX method that called your button. Stack traces are always from botton to top. So the first error at the bottom of the stack is the one that started the exception, and it propagated to the top till the code calls the jvm to notify that it could not recover from the error. – MissingSemiColon Oct 26 '17 at 07:20
  • @MissingSemiColon So, how do you suggest, I should solve the NullPointerException? – Ronit Oct 26 '17 at 07:32
  • What is in line 42? I suppose its a variable that has a value of null and you are trying to call a method on it. – MissingSemiColon Oct 26 '17 at 07:34
  • 1
    The substring `loginButtonCliked` is not in the fxml with the `Label` with `fx:id="alertLabel"`, so you're using the same controller class for multiple fxmls. ***Different controller instances*** are used in this case and obviously the other fxml does not contain a `Label` with `fx:id="alertLabel"` so you get the NPE. – fabian Oct 26 '17 at 07:50
  • @fabian Please read the fxml code carefully. It has the fx:id="alertLabel". – Ronit Oct 26 '17 at 08:30
  • @MissingSemiColon bro, that what I said earlier, line 42 is : alertLabel.setText("..."); – Ronit Oct 26 '17 at 08:31
  • Well then alerLabel is null, that is for sure. If you are using eclipse refresh the project, fxml files do not get undated automatically most of the time. Had a lot of problem with that. Add a if(alertLabel != null) before an see if you still get an exception. – MissingSemiColon Oct 26 '17 at 08:49
  • 1
    There is nothing in this FXML file with `loginButtonClicked` as the handler method. How is `loginButtonClicked()` getting called? – James_D Oct 26 '17 at 10:49
  • @Ronit please read the fxml and my comment carefully. It's the `loginButtonCliked` handler that is missing from the fxml and not invoked from another method in your controller. This means there is some other fxml that uses this handler which means there are 2 controller instances: one where `alertLabel` in non-`null` and a different one where `alertLabel` is `null` and the event handler is invoked... – fabian Oct 26 '17 at 11:08
  • @fabian yes there are other fxml documents, referring to the same controller. But, alertLabel is present in only one fxml file. And it's value is not null. The label already has some stuff written. – Ronit Oct 26 '17 at 11:20
  • @James_D loginButtonClicked is being called in another fxml file, and this one acts as a alert box. – Ronit Oct 26 '17 at 11:22
  • @Ronit But that means `alertLabel` is initialized in one controller, and you are calling `loginButtonClicked` on a different controller. `alertLabel` is not initialized in the controller on which you are calling `loginButtonClicked`, so it is still null and you get a null pointer exception. Fabian has already explained all this (twice). – James_D Oct 26 '17 at 11:28
  • @MissingSemiColon OK, Yes alertLabel is indeed null. alertLabel.getText() is not though. Can you please tell me how can I make alertLabel, not null? – Ronit Oct 26 '17 at 11:28
  • @James_D Yes alertLabel is not initialized. So, can you tell me how I can initialize alertLabel. Just FYI, alertLabel.getText() is not null, but alertLabel seems to be null. – Ronit Oct 26 '17 at 11:30
  • Start by using a different controller class for each FXML file. It is too difficult to keep track of what is initialized and what is null in any particular instance of the class otherwise. Then pass the value you need from one controller to another, using techniques in https://stackoverflow.com/questions/14187963 – James_D Oct 26 '17 at 11:32
  • BTW "Just FYI, `alertLabel.getText()` is not null, but `alertLabel` seems to be null." doesn't make any sense at all (and suggests you don't really understand what a null pointer exception is). If `alertLabel` is null, you can't even call `alertLabel.getText()`; it just throws an exception. Read and understand https://stackoverflow.com/questions/218384 – James_D Oct 26 '17 at 11:35
  • Try adding this `@FXML private void initialize() { System.out.println("Controller initialized"); } `does it print the text? I see you have okButtonCliked method in fxml but no FXML tag on the method, where are you calling loginButtonCliked from?. are you loading the class with `FXMLLoader loader = new FXMLLoader(); loader.setLocation(Main.class.getResource("/fxml/example.fxml"));loader.load();` ? – MissingSemiColon Oct 26 '17 at 12:24

0 Answers0