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();
}
}