0

I made a similar question yesterday but I think it wasnt well explained, so I wanted to ask it again but with some changes I made in my code. I apologise if i write too much but i want to make everything understandable.

So, Im making a pokemon simulator where you can catch and train pokemon. I have one main fxml file which contains buttons to access to the diferent fxml files like catch, battle, shop, bag... Yesterday i was doing the bag where all your items will be stored.

Everything is working fine and the windows are switching between them properly. The problem comes when i was trying to add a Label to each item in the bag, which was suposed to show the user the cuantity he has of each item. So i created all the Labels with no text so they are empty.

They will get filled from the information i get from a database, this thing also works properly, i connect to the db and get the item cuantity. The problem comes when i want to show that item cuantity in my bag window.

Why? Because as you can imagine, i want that when you click on the bag button, the bag file loads with all the Labels filled with the cuantity of each item. The Labels are defined on the bag fxml controller, so if i want to fill them with some text, i cant do it from my main windows which uses another controller, i need to do it throught the bag controller.

This is the code i tried to make it work(Located in main controller):

@FXML
    void mochila (ActionEvent event) throws IOException, SQLException {
        AnchorPane pane = FXMLLoader.load(getClass().getResource("mochila.fxml"));
        anchorPaneContent.getChildren().setAll(pane);
        anchorPane2.setStyle("-fx-background-color: #3f3f3f;");
        cm.getCantidad();
    }

getCantidad is a function that i have in my bag controller and this is it:

public void getCantidad() {
        lblpokeballCount.setText("Cantidad: "+pokeballcount);
        lblsuperballCount.setText("Cantidad: "+superballcount);
        lblultraballCount.setText("Cantidad: "+ultraballcount);
        lblmasterballCount.setText("Cantidad: "+masterballcount);
    }

So when i try to run this function from the main controller, it returns me null pointer exception. That means the labels are not initialized but when i type first AnchorPane pane = FXMLLoader.load(getClass().getResource("mochila.fxml"));Shoudlnt all the resources from the file be loaded? Because i creaded a button in my bag file, when on clicked runs the same function, and it works correctly because im calling it from the same controller/file.

So now i dont really know what to do, this is a proyect for school but my programing teachers have never touched javafx so they dont even know what im doing. You are my only hope. I tried to understand this post: post

But i dont understand it at all as im new to all this stuff. So guys if you can help me i would be really gratefull, thanks!

edit:

@FXML
    void mochila (ActionEvent event) throws IOException, SQLException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("mochila.fxml"));
        anchorPaneContent.getChildren().setAll(loader);
        controladorMochila controller = loader.<controladorMochila>getController();
        controller.getCantidad();
    }

anchorPaneContent is an anchorpane thats inside the main pane. All the buttons are in the main pane, and depending on the button you click, anchorpanecontent will change for another fxml file. I tried to do it like in the post i mentioned above. But i cant do anchorPaneContent.getChildren().setAll(loader); because it says: the node setAll is not aplicable for the arguments(FXMLLoader)

amurin
  • 3
  • 3
  • Haven't we [already been through this](https://stackoverflow.com/q/49889984)? What is `cm` here? (Whatever it is, it is clearly not the controller that is created when you load `mochila.fxml`.) – James_D Apr 18 '18 at 19:25
  • Hi, yes we did but now i've made a different controller for each fxml file like you said. cm is this: controladorMochila cm = new controladorMochila(); Its an object i create from controladorMochila which is the controller of my bag fxml file – amurin Apr 18 '18 at 19:28
  • Right, so `cm` is just an object you create. It is not the controller. (`ControladorMochila` is not the controller either; it's a class. The controller is a specific object.) Why don't you try it the same way that it is done in the first option in the post you linked? – James_D Apr 18 '18 at 19:29
  • `@FXML void mochila (ActionEvent event) throws IOException, SQLException { FXMLLoader loader = new FXMLLoader(getClass().getResource("mochila.fxml")); anchorPaneContent.getChildren().setAll(pane); anchorPane2.setStyle("-fx-background-color: #3f3f3f;"); controladorMochila controller = loader.getController(); controller.getCantidad(); }` I tried this but now how can i set anchorPaneContent.getChildren().setAll(loader); because it needs to be a pane – amurin Apr 18 '18 at 19:35
  • Please [edit] the question with the additional information (it's too hard to read in the comments). I can't see anywhere you are actually loading the FXML in the code in the comment. Note we don't really know what `anchorPaneContent` and `anchorPane2` are here. – James_D Apr 18 '18 at 19:37
  • What I recommend is that you create a very simple example (from scratch) that has two FXML files with corresponding controllers, say with just a button in the first one that causes the second to be loaded and sets the text of a label in the second FXML file. That will probably let you figure out what you are doing wrong yourself (because you will focus on the task at hand, instead of having lots of other "clutter" around). And if not, you can post the entire small example (FXML files and all) here. – James_D Apr 18 '18 at 19:47
  • @James_D I edited the post so its more understandable now. And yes maybe i could make a simple proyect and test it, but it comes to be the same because the code that im making for changing the Label text are like 10 lines, im not using anything else from my code excepting the panes. So i explained above what does each pane. And thanks for helping me, it means a lot. – amurin Apr 18 '18 at 20:07

1 Answers1

0

You are trying to add an FXMLLoader to an anchor pane, which will not work, since an FXMLLoader is not a visual component (it is a thing that loads FXML files).

Additionally, you are trying to get the controller from the FXMLLoader without actually loading the FXML file; this won't work because the controller class is specified in the FXML file (so the FXMLLoader doesn't know what kind of controller to create until it loads the file).

You need to load the FXML file and add the result to the anchor pane:

@FXML
void mochila (ActionEvent event) throws IOException, SQLException {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("mochila.fxml"));
    anchorPaneContent.getChildren().setAll(loader.load());
    controladorMochila controller = loader.<controladorMochila>getController();
    controller.getCantidad();
}

or, if you want to be a bit more explicit:

@FXML
void mochila (ActionEvent event) throws IOException, SQLException {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("mochila.fxml"));
    Parent pane = loader.load();
    anchorPaneContent.getChildren().setAll(pane);
    controladorMochila controller = loader.<controladorMochila>getController();
    controller.getCantidad();
}
James_D
  • 201,275
  • 16
  • 291
  • 322
  • Man I cant believe it, thanks!!!! You finally solved my problem, and it was easyer than i thought + now i understand everything better thanks to you. I mean really thank you :D, that was a really important part in my program because i will need to apply it more than once. Thank you again for helping me and thank you for your time! – amurin Apr 18 '18 at 20:25