0

I'm trying to build a simple MVC app with JavaFx. It takes the value of the left TextField (tf1) and copies it into the right one (tf2) when you hit button b. So, when I define what to do when Button b is clicked, eclipse doesn't show an error, but when I run the programme instead of returning the button a NullpointerException is thrown.

Do you have an idea about what I'm doing wrong?

Thanks in advance!

Model.java:

package mvc;

public class Model {
private String firsttext;
private String lasttext;

public String getFirsttext() {
    return firsttext;
}
public void setFirsttext(String firsttext) {
    this.firsttext = firsttext;
}
public String getLasttext() {
    return lasttext;
}
public void setLasttext(String lasttext) {
    this.lasttext = lasttext;
}

}

View.java:

package mvc;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class View extends Application {
private TextField tf1;
private TextField tf2;
private Button b;

@Override
public void start(Stage stage) {
    tf1 = new TextField();
    tf2 = new TextField();
    b = new Button("Copy");

    FlowPane fp = new FlowPane();
    fp.getChildren().addAll(tf1, b, tf2);

    Scene scene = new Scene(fp, 600, 200);
    stage.setScene(scene);
    stage.show();
}

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

public TextField getTf1() {
    return tf1;
}

public void setTf1(TextField tf1) {
    this.tf1 = tf1;
}

public TextField getTf2() {
    return tf2;
}

public void setTf2(TextField tf2) {
    this.tf2 = tf2;
}

public Button getB() {
    return b;
}

public void setB(Button b) {
    this.b = b;
}
}

Controller.java:

package mvc;

public class Controller {
private View view;
private Model model;

public Controller(View v, Model m) {
    view = v;
    model = m;
}

public void initController() {
    view.getB().setOnAction(evt -> {
        model.setFirsttext(view.getTf1().getText());
        model.setLasttext(model.getFirsttext());

        view.getTf2().setText(model.getLasttext());
    });

}
}

App.java:

package mvc;

public class App {
public static void main(String[] args) {
    Model m = new Model();
    View v = new View();
    Controller c = new Controller(v, m);
    v.init(args);
    c.initController();
}
}
  • Please append a stacktrace here – eugene-nikolaev Jun 05 '17 at 07:11
  • 1
    Take a look at [this question](https://stackoverflow.com/questions/33303167/javafx-can-application-class-be-the-controller-class) - it is not exactly the same, but the answer explains the intended use of the `Application` class. Calling `launch` creates a new instance, so the one you are seeing isn't the one you pass on to your controller. It is best to make the `App` class extend application, and only call `launch` from a static context. – Itai Jun 05 '17 at 07:16
  • The stacktrace is: Exception in thread "main" java.lang.NullPointerException at mvc.Controller.initController(Controller.java:14) at mvc.App.main(App.java:9) – ILikeC0ding Jun 05 '17 at 07:27

1 Answers1

0

Look at sillyfly's comment. I made a new instance that was not passed on the controller.

Here are the corrected files:

Model.java -> didn't change, take a look above to see it
Controller.java -> didn't change, take a look above to see it

View.java:

package mvc;

import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;

public class View  {
private TextField tf1;
private TextField tf2;
private Button b;
private Scene scene;

public View() {
    tf1 = new TextField();
    tf2 = new TextField();
    b = new Button("Copy");

    FlowPane fp = new FlowPane();
    fp.getChildren().addAll(tf1, b, tf2);

    scene = new Scene(fp, 600, 200);
}

public Scene getScene() {
    return scene;
}

public void setScene(Scene scene) {
    this.scene = scene;
}

public TextField getTf1() {
    return tf1;
}

public void setTf1(TextField tf1) {
    this.tf1 = tf1;
}

public TextField getTf2() {
    return tf2;
}

public void setTf2(TextField tf2) {
    this.tf2 = tf2;
}

public Button getB() {
    return b;
}

public void setB(Button b) {
    this.b = b;
}
}

App.java:

package mvc;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class App extends Application {
private Scene scene;
private Model m = new Model();
private View v = new View();
Controller c = new Controller(v, m);

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

@Override
public void start(Stage stage) throws Exception {
    scene = v.getScene();
    stage.setScene(scene);
    stage.show();
    c.initController();
}
}