JAVA 引用对象形式 class A in other class
JAVA Reference to object form class A in other class
我有一个 class:
public class RectangularSection {
public double widthRectangle;
public double getWidthRectangle() {
return widthRectangle;
}
public void setWidthRectangle(double widthRectangle) {
this.widthRectangle = widthRectangle;
}}
和 class 对象 "section":
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 的对象 "section" class
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()));
}}
我读到了这个,我也做了同样的事情,但它不起作用我做错了什么?
..................................................... .....................
代码改进的范围很大,但我不会完全重构代码。
对于您的缺陷,您没有设置 widthRectangle
字段,而是试图通过 .getWidthRectangle().getText()
获取它,这会抛出 NullPointerException
要解决它,请在 sendStrengthClassOfConcrete()
方法中使用它之前设置 widthRectangle
值。
private void sendStrengthClassOfConcrete() {
object2.setRectangularSection(section);
TextField someNumberTextField = new TextField("24");
object2.setWidthRectangle(someNumberTextField);
object2.section.setWidthRectangle(Double.parseDouble(object2
.getWidthRectangle().getText()));
我有一个 class:
public class RectangularSection {
public double widthRectangle;
public double getWidthRectangle() {
return widthRectangle;
}
public void setWidthRectangle(double widthRectangle) {
this.widthRectangle = widthRectangle;
}}
和 class 对象 "section":
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 的对象 "section" class
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()));
}}
我读到了这个,我也做了同样的事情,但它不起作用我做错了什么? ..................................................... .....................
代码改进的范围很大,但我不会完全重构代码。
对于您的缺陷,您没有设置 widthRectangle
字段,而是试图通过 .getWidthRectangle().getText()
获取它,这会抛出 NullPointerException
要解决它,请在 sendStrengthClassOfConcrete()
方法中使用它之前设置 widthRectangle
值。
private void sendStrengthClassOfConcrete() {
object2.setRectangularSection(section);
TextField someNumberTextField = new TextField("24");
object2.setWidthRectangle(someNumberTextField);
object2.section.setWidthRectangle(Double.parseDouble(object2
.getWidthRectangle().getText()));