【问题标题】:JavaFx MVC Getter throws NullPointerExceptionJavaFx MVC Getter 抛出 NullPointerException
【发布时间】:2017-11-05 21:41:19
【问题描述】:

我正在尝试使用 JavaFx 构建一个简单的 MVC 应用程序。当您点击按钮 b 时,它将获取左侧 TextField (tf1) 的值并将其复制到右侧 (tf2) 中。因此,当我定义单击按钮 b 时要执行的操作时,eclipse 不会显示错误,但是当我运行程序而不是返回按钮时会抛出 NullpointerException。

你知道我做错了什么吗?

提前致谢!

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

【问题讨论】:

  • 请在此处附加堆栈跟踪
  • 看看this question - 它不完全相同,但答案解释了Application 类的预期用途。调用launch 会创建一个新实例,因此您看到的不是您传递给控制器​​的那个。最好让App 类扩展应用程序,并且只从静态上下文中调用launch
  • 堆栈跟踪是:线程“main”中的异常 java.lang.NullPointerException at mvc.Controller.initController(Controller.java:14) at mvc.App.main(App.java:9)

标签: java model-view-controller javafx nullpointerexception


【解决方案1】:

看看 sillyfly 的评论。我创建了一个未在控制器上传递的新实例。

以下是更正后的文件:

Model.java -> 没变,上图看看
Controller.java -> 没变,上图看看

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

【讨论】:

    猜你喜欢
    • 2018-08-13
    • 2017-08-11
    • 2013-09-07
    • 2013-06-18
    • 2016-07-07
    • 2014-10-02
    • 2012-05-21
    • 2014-08-15
    • 2013-08-06
    相关资源
    最近更新 更多