0

I can't figure out a way to get my program work. How can I fix the issue?

Main.java

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;


public class Main extends Application {

    private Stage window;

    public void start(Stage window) throws Exception {
        this.window = window;
        this.window.setTitle("test");

        initLayouts();
    }

    public void initLayouts() throws Exception{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(ClassLoader.getSystemResource("sample.fxml"));
        BorderPane rootLayout = (BorderPane)loader.load();

        Scene scene = new Scene(rootLayout);
        window.setScene(scene);
        window.show();

        loader = new FXMLLoader();
        loader.setLocation(ClassLoader.getSystemResource("content.fxml"));
        AnchorPane ap = (AnchorPane)loader.load();

        rootLayout.setCenter(ap);
        ContentController controller = loader.getController();
        controller.setMain(this);

    }

    public void showEditWindow() throws Exception{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(ClassLoader.getSystemResource("editWindow.fxml"));
        AnchorPane ap = (AnchorPane)loader.load();

        Stage editWindow = new Stage();
        editWindow.setTitle("Add");
        editWindow.initModality(Modality.WINDOW_MODAL);
        editWindow.initOwner(window);

        Scene scene = new Scene(ap);
        editWindow.setScene(scene);

        EditController controller = loader.getController();
        controller.setStage(editWindow);

        editWindow.showAndWait();

    }


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

EdiController.java

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.io.IOException;


public class EditController{

    @FXML
    TextField textField = new TextField();
    @FXML
    Button button;

    private Stage stage;
    private ContentController contentController;

    public void setStage(Stage stage){
        this.stage = stage;
    }

    @FXML
    private void buttonClicked() throws IOException {
        assert(contentController != null);
        assert(textField != null);
        assert(button != null);
        contentController.setLabel(textField.getText());
        stage.close();

    }
}

ContentController.java

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;

public class ContentController {

    @FXML
    private Button add;
    @FXML
    private Button clear;
    @FXML
    public Label label;

    private Main main;

    public void setMain(Main main){
        this.main = main;
    }

    public void setLabel(String label){
        this.label.setText(label);
    }

    @FXML
    private void clearLabel(){
        label.setText("");
        System.out.println("text removed");
    }

    @FXML
    private void addClicked() throws Exception {
        main.showEditWindow();

    }
}

The main issue seems to be on the lane of EditController.java 'contentController.setLabel(textField.getText());' this is what always causes the issue. What options do I have to fix it? Thanks in advance

Jiri
  • 115
  • 8
  • 4
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Lavaman65 Mar 06 '17 at 15:19
  • Where are you initializing your `contentController`? – UnholySheep Mar 06 '17 at 15:20
  • @UnholySheep It's initialized in my Main class. – Jiri Mar 06 '17 at 15:34
  • Do you mean `ContentController controller = loader.getController();`? Because that is a different `ContentController` than the one used in the `EditController` class – UnholySheep Mar 06 '17 at 15:38
  • @UnholySheep You're right. I'm aware of it. The contentController is just an object of ContentController class. Basically, I'm trying to get an access to the label which is in another class called ContentController. It seems like EditController cannot see the label which is placed in another class. I can't think of a way to link the two classes so that they can communicate between each other. – Jiri Mar 06 '17 at 15:45
  • The `contentController` (in `EditController`) is not an object, because it's never initialized. I'm not sure why you want to have a single instance shared between various classes, but you have several options, including make it a singleton, creating some manager class that stores the instance (e.g.: your `Main` class) or storing it as a member in `Main` and passing it to the `EditController`. – UnholySheep Mar 06 '17 at 15:51
  • @UnholySheep Ah. I see. Well, I don't insist on having it shared between various classes. I'd just love to make it work in some way. – Jiri Mar 06 '17 at 16:14

0 Answers0