0

I have some labels i created in a FXML file and i want to change the text displayed in the label based off a name in an array, but whenever i try the label.setText() i get nullPointerException, and also when i try to do that for a text object or anything else that youre able to set the text to and i want to know what it is im doing wrong. This is the code for the java class which im trying to overwrite the vales for This is the main class that sets off the application. I call this first one in main and depending on what the user inputs that determines what gets loaded in the next scene

@FXML private void handleLogin(ActionEvent event)
    {
        PersonnelController control = new PersonnelController();
        Fleet spaceship = new Fleet("test");
        spaceship.loadStarShips("data/fleet.csv");
        spaceship.loadCrewMembers("data/personnel.csv");
        User prompt = new User(userName.getText(),password.getText());
        prompt.loadUsers("data/users.csv");
        if(prompt.validate(userName.getText(),password.getText()) == true)
        {
            output.setText("success");


            loadSecond(event);
            control.loadClasses(userName.getText());
        }
        else
        {
            output.setText("Incorrect password");
        }
    }
    @FXML private void loadSecond(ActionEvent event)
    {
        try 
        {
            AnchorPane pane = FXMLLoader.load(getClass().getResource("/Personnel.fxml"));
            root.getChildren().setAll(pane);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

This is the second class that gets loaded after the first

public class PersonnelController {

    private Fleet spaceCadet;
    @FXML private AnchorPane person;
    @FXML Button button2;
    @FXML Text name;

    public void loadClasses(String string) 
    {
        spaceCadet = new Fleet("window2");
        spaceCadet.loadStarShips("data/fleet.csv");
        spaceCadet.loadCrewMembers("data/personnel.csv");


        int i;
        string = string.toLowerCase();

        String value = spaceCadet.getStarshipsByRegistry(string);

        String [] breaker = value.split("\n");

        for( i = 0; i < breaker.length; i++)
        {
            System.out.println(breaker[i]);
            if(breaker[i].contains("commanding officer"))
            {
                System.out.println(breaker[i]);

                name.setText(breaker[i]);//this is the line that throws the error
            }
            //break;
        }
    }

and here is the FXML file:

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

<?import javafx.scene.image.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane fx:id="person" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.controller.PersonnelController">
   <children>
      <Label fx:id="welcomeMessage" layoutX="218.0" layoutY="20.0" text="Welcome Captain">
         <font>
            <Font size="19.0" />
         </font>
      </Label>
      <Button fx:id="button2" layoutX="477.0" layoutY="13.0" mnemonicParsing="false" onAction="#loadFirst" text="Log Out">
         <font>
            <Font size="19.0" />
         </font>
         <textFill>
            <LinearGradient endX="1.0" endY="0.9289099526066351">
               <stops>
                  <Stop color="#2451c3" />
                  <Stop color="#220606" offset="1.0" />
               </stops>
            </LinearGradient>
         </textFill>
      </Button>
      <ImageView fx:id="captain" fitHeight="139.0" fitWidth="114.0" layoutX="18.0" layoutY="63.0" pickOnBounds="true" preserveRatio="true" />
      <ImageView fitHeight="139.0" fitWidth="114.0" layoutX="161.0" layoutY="63.0" pickOnBounds="true" preserveRatio="true" />
      <ImageView fitHeight="139.0" fitWidth="114.0" layoutX="316.0" layoutY="63.0" pickOnBounds="true" preserveRatio="true" />
      <ImageView fitHeight="139.0" fitWidth="114.0" layoutX="456.0" layoutY="63.0" pickOnBounds="true" preserveRatio="true" />
      <ImageView fitHeight="139.0" fitWidth="114.0" layoutX="18.0" layoutY="225.0" pickOnBounds="true" preserveRatio="true" />
      <ImageView fitHeight="139.0" fitWidth="114.0" layoutX="161.0" layoutY="225.0" pickOnBounds="true" preserveRatio="true" />
      <ImageView fitHeight="139.0" fitWidth="114.0" layoutX="316.0" layoutY="225.0" pickOnBounds="true" preserveRatio="true" />
      <ImageView fitHeight="139.0" fitWidth="114.0" layoutX="456.0" layoutY="225.0" pickOnBounds="true" preserveRatio="true" />
      <TextArea fx:id="commandingOfficer" editable="false" layoutX="18.0" layoutY="203.0" prefHeight="22.0" prefWidth="114.0" />
      <TextArea fx:id="firstOfficer" editable="false" layoutX="156.0" layoutY="202.0" prefHeight="24.0" prefWidth="125.0" />
      <TextArea fx:id="communicationsOfficer" editable="false" layoutX="310.0" layoutY="202.0" prefHeight="24.0" prefWidth="125.0" />
      <TextArea fx:id="chiefEngineeringOfficer" editable="false" layoutX="451.0" layoutY="203.0" prefHeight="22.0" prefWidth="125.0" />
      <TextArea fx:id="helmsman" editable="false" layoutX="18.0" layoutY="365.0" prefHeight="22.0" prefWidth="114.0" />
      <TextArea fx:id="navigator" editable="false" layoutX="156.0" layoutY="365.0" prefHeight="22.0" prefWidth="114.0" />
      <TextArea fx:id="chiefMedicalOfficer" editable="false" layoutX="310.0" layoutY="364.0" prefHeight="24.0" prefWidth="125.0" />
      <TextArea fx:id="nurse" editable="false" layoutX="451.0" layoutY="364.0" prefHeight="24.0" prefWidth="125.0" />
      <Text fx:id="name" layoutX="373.0" layoutY="36.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Text" wrappingWidth="103.33935546875" />
   </children>
</AnchorPane>

Any help would be appreciated so that i can fix this error ive been running into semi frequently.

  • Not necessarily because whenever i run the application i can see the Label and i have it equal to just "text" in the scenebuilder but when i try to write over it and do the label.setText() call i get the NullPointerException. – Antonio Campos Apr 24 '20 at 04:25
  • Please post [mre] and the full stacktrace. Mark the line which throws the error with a comment. – c0der Apr 24 '20 at 05:15
  • when is your `loadClasses` called? FXML components are only instantiated with the `initialize()` method. – Amir Schnell Apr 24 '20 at 07:02
  • I calll this method in another class which is the first scene with a different fxml file, when certain requirements are met i call the loadClasses function and i call the next scene which is this one ive posted. – Antonio Campos Apr 24 '20 at 07:17
  • `PersonnelController control = new PersonnelController();` creates a instance of the controller class that `FXMLLoader` never gets its hands on. Only if the `FXMLLoader` has access to the controller instance when its `load` method is called, its able to inject the fields; this is clearly not the case here; you don't even load the fxml in the same method, but in another event handler method. – fabian Apr 24 '20 at 08:05

0 Answers0