2

So i've been playing around with javaFx and FXML and i keep running into a problem where i get errors when using a conditional statement with something like a .getText() method and trying to reference an FXML variable and its Items/children from within the bounds of this.

Take for example:

 Public Void checkTextField(TextField txt, AnchorPane pane){
   //#1
   system.out.println(pane.getItems());

   if(txt.getText() != null && !txt.getText().isEmpty()){
   //#2
   system.out.println(pane.getItems());
   }
 }

In this example #1 always works for me and will print out a list of what i've got in my pane (Or any container for that matter), whereas with #2 i always seem to get a NullPointerException and a InvocationTargetException if anyone knows why this could be, it would be great to find out what i'm doing wrong here or what i could add to make it work

cheers

Edit:

Errors:

 Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.event.Event.fireEvent(Unknown Source)
    at javafx.scene.Node.fireEvent(Unknown Source)
    at javafx.scene.control.Button.fire(Unknown Source)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.event.Event.fireEvent(Unknown Source)
    at javafx.scene.Scene$MouseHandler.process(Unknown Source)
    at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
    at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$139/1713797504.get(Unknown Source)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
    at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
    at com.sun.glass.ui.View.notifyMouse(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$$Lambda$36/2117255219.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.Trampoline.invoke(Unknown Source)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
    ... 55 more
Caused by: java.lang.NullPointerException
    at fxml.MainController.setSubjName(MainController.java:66)
    ... 64 more

Here is also a more exact example of what i'm trying to achive:

public void setName(ActionEvent event){ 
    if(nameTxtField != null && nameTxtField.getText() != null && !nameTxtField.getText().isEmpty()){
        Button.setVisible(false);
        nameTxtField.setVisible(false);
        retry.setVisible(false);
        //Testing to get reference to mainVBox's children
        System.out.println(mainVbox.getChildren());
        paneLbl.setText(NameTxtField.getText());

    }else{
        retry.setText("Please try again.");
    }
}

Edit #2:

Declaration of annotated variable in Controller and FXML:

Controller:

    @FXML
    TextField nameTxtField;
    @FXML
    VBox mainVbox;

FXML:

 <TextField fx:id="nameTxtField" layoutX="14.0" layoutY="14.0" onAction="#setName" prefHeight="32.0" prefWidth="246.0" promptText="Subject Name" />

 <VBox fx:id="mainVbox" layoutX="13.0" layoutY="109.0" prefHeight="355.0" prefWidth="212.0" spacing="25.0">
ProvingBard
  • 333
  • 4
  • 12
  • 1
    Just a tiny hint: Try to use curley brackets after an if statement to clearly show which lines should be processed if the condition is true: if(condition){ system.out.println(pane.getItems()); } – Luke May 04 '15 at 06:50
  • 1
    It means the parameter "txt" is not initialized yet, i.e. it is null. Show the code where the txt is defined. The TextField in fxml file must have a fx:id what you defined in the controller. – Uluk Biy May 04 '15 at 06:50
  • @UlukBiy My problem is i have already defined the variable in my controller and it still gives me the exception, i'll have another thorough look at it but i have referenced the `txt` parameter in this method when i was testing for the problem and it works using the `getText()` method to do something simple such as print out what was entered. The problem seems to have to do with the `.getItems` segment and actually trying to reference this inside an if statement – ProvingBard May 04 '15 at 07:08
  • @Luke Sorry i forgot to include the curly brackets in my example, but they were in the actual program itself, but thanks for spotting that for me – ProvingBard May 04 '15 at 07:09
  • @ProvingBard Can you show us how you have declared `txt` in your controller and its equivalent declaration in the fxml as well? – ItachiUchiha May 04 '15 at 07:16
  • @ProvingBard, post the full error log you have had. – Uluk Biy May 04 '15 at 07:17
  • The cause of the error is at line 66 of MainController.java. Which line of code exists there? – Uluk Biy May 04 '15 at 07:24
  • @ItachiUchiha I've added the declaration lines to the question – ProvingBard May 04 '15 at 07:26
  • @UlukBiy that is the line where it says : `System.out.println(mainVbox.getChildren());` – ProvingBard May 04 '15 at 07:27
  • The @ FXML annotated variable name must match the fx:id in fxml file. So change nameTxtField to subjNameTxtField in the controller. Or change fx:id to fx:id="nameTxtField" in fxml file. Do it to all @ FXML annotated variables. – Uluk Biy May 04 '15 at 07:30
  • @UlukBiy Sorry i got that from an older version of the FXML file, i just updated it to the one i am currently using, i have checked though, that the fx:id and any annotated variables match up – ProvingBard May 04 '15 at 07:32
  • @ProvingBard, ah ok. Then mainVbox seems not to be initialized. You need to post its declaration also. – Uluk Biy May 04 '15 at 07:33
  • @UlukBiy I've added it to the Edit #2 section, also, i don't know if it would change anything but the VBox and TextField are from different FXML files, Although they are both linked to the same controller and i can get reference of the VBox's children just not from within an if statement where i am testing the textField for not null or something. – ProvingBard May 04 '15 at 07:40
  • @ProvingBard if you are not using the setController() method of FXMLLoader prior to invoking its load() method, then you are having 2 instances of that controller, in which one of them you will have nameTxtField as null, and in another the mainVbox as null. As a solution you may use fx:include to include the fxml into second one and use/define just one controller for parent fxml. – Uluk Biy May 04 '15 at 07:49
  • @UlukBiy So how would i go about using the setController() method in my code? in the `parent = FXMLLoader.load(getClass().getResource())` segment? or somewhere else? or should i leave my controller as is and use the fx:include, if so where would i put this bit of code? – ProvingBard May 04 '15 at 08:04
  • @ProvingBard, I think until getting more familiar with JavaFX and FXML, it is more convenient defining 2 different controllers for 2 different fxml. – Uluk Biy May 04 '15 at 08:25
  • @UlukBiy Probably true haha, i will create another controller and see if that works, but if i want to reference something from one controller in another controller, in this case, the textfield/VBox, would i have to import anything or just use it as a parameter in my methods etc. – ProvingBard May 04 '15 at 08:29
  • @ProvingBard, review [this serach results](http://stackoverflow.com/search?q=%5Bjavafx%5D+controller+parameter+is%3Aquestion) if these help. Also see [Accessing FXML controller class](http://stackoverflow.com/questions/10751271/accessing-fxml-controller-class). – Uluk Biy May 04 '15 at 08:51
  • I recommend *always* having a different controller for each FXML file (no matter how much JavaFX experience you have). – James_D May 04 '15 at 11:10
  • @James_D I'll definitely take that into consideration, and from what i've been looking at, he best way to communicate between conrollers is with model classes right? – ProvingBard May 04 '15 at 11:32

0 Answers0