JAVA在其他类中引用对象形式A类

时间:2016-07-13 14:38:42

标签: java javafx fxml

我有一个班级:

public class RectangularSection {

public double widthRectangle;

public double getWidthRectangle() {
    return widthRectangle;
}

public void setWidthRectangle(double widthRectangle) {
    this.widthRectangle = widthRectangle;
}}

带对象的课程"部分":

public class RectangleController implements Initializable {

RectangularSection section = new RectangularSection();

public RectangularSection getObject(){
    return section;
}

public void setRectangularSection(RectangularSection obj){
    this.section = obj;
}

@FXML
private TextField widthRectangle;

public TextField getWidthRectangle() {
    return widthRectangle;
}

public void setWidthRectangle(TextField widthRectangle) {
    this.widthRectangle = widthRectangle;
}

@Override
public void initialize(URL arg0, ResourceBundle arg1) {
}}

我需要对象"部分"来自此类

中的RectangleController
public class Controller implements Initializable  {

RectangularSection section = new RectangularSection();
RectangleController object2 = new RectangleController();

@FXML
private Label szer;

    @FXML
private void sendStrengthClassOfConcrete() {
    object2.setRectangularSection(section);
    object2.section.setWidthRectangle(Double.parseDouble(object2.getWidthRectangle().getText()));
    szerokosc.setText(String.valueOf(object2.getObjekt().getWidthRectangle()));


}}

我读到了这个,我也做了同样的事情,但它干扰了我的工作做错了什么? .................................................. .....................

1 个答案:

答案 0 :(得分:0)

代码改进的范围很广,但我不会全面重构代码。

对于您的缺陷,您没有设置widthRectangle字段,并且您正试图通过投掷.getWidthRectangle().getText()

NullPointerException获取该字段

要解决此问题,请在widthRectangle方法中使用之前设置sendStrengthClassOfConcrete()值。

private void sendStrengthClassOfConcrete() {
        object2.setRectangularSection(section);
        TextField someNumberTextField = new TextField("24");
        object2.setWidthRectangle(someNumberTextField);
        object2.section.setWidthRectangle(Double.parseDouble(object2
                .getWidthRectangle().getText()));