英文:
Cannot get both radio buttons to pass values to ObaservableList Java?
问题
以下是您提供的Java代码的翻译部分:
我正在学习Java的过程中,似乎无法使这个函数工作。我正在为一个库存系统的项目工作,其中零件添加到一个可观察列表中。在添加零件的界面上,我有两个单选按钮,用于选择零件是InHouse制造还是OutSourced制造。我使用切换组的if语句来检查选中哪个单选按钮。我尝试使用else语句来传递值,如果它是外包零件。现在,如果单选按钮设置为InHouse,它会正确传递值,但是如果设置为Outsource,它会在if语句所在的行抛出空指针异常。
以下是我试图使其工作的代码。
// 这里是您的Java代码的一部分,已翻译为中文
请注意,这只是代码的一部分翻译。如果您需要更多帮助或有其他问题,请随时提出。
英文:
I am in the process of learning Java and I can't seem to make this function work. I am working on a project for an inventory system. Where parts are added to an observable list. In the add part screen, I have two radio buttons to select whether the part is an InHouse made or OutSourced. I have an if statement using a toggle group to check which radio button is selected. I was trying to use an else statement to pass the values if it was an outsourced part. Right now it will pass the values correctly if the radio button is set to InHouse, but when set to Outsource it throws a null pointer exception at the line the if statement is on.
Here is the code I am trying to make work.
package ViewController;
import Model.InHouse;
import Model.Inventory;
import Model.OutSource;
import Model.Part;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;
public class AddPart implements Initializable {
public RadioButton addPartInHouse;
public TextField partIDTxtbox;
public TextField partNameTxtbox;
public TextField invTxtbox;
public TextField pricePerUnitTxtbox;
public TextField maxQtyTxtbox;
public TextField minQtyTxtbox;
public Button addPartSaveButton;
public Button addPartCancelButton;
public RadioButton addPartOutSource;
public Label companyNameLabel;
public TextField companyNamePrompt;
public ToggleGroup switchfield;
public void addPartInHouse(ActionEvent actionEvent) {
companyNameLabel.setText("Machine ID");
companyNamePrompt.setPromptText("Machine ID");
addPartOutSource.setSelected(false);
}
public void addPartOutsource(ActionEvent actionEvent) {
companyNameLabel.setText("Company Name");
companyNamePrompt.setPromptText("Company Name");
addPartInHouse.setSelected(false);
}
public void addPartSaveButton(ActionEvent e) throws IOException {
/*this is my line 55 below*/
if (this.switchfield.getSelectedToggle().equals(this.addPartInHouse)) {
Part partadd = new InHouse(0, "", 0.0, 0,0,0,0);
if(partIDTxtbox.getText().isEmpty()){
partadd.setPartID(Inventory.getPartIDCount());
}
if (!partNameTxtbox.getText().isEmpty()){
partadd.setPartName(partNameTxtbox.getText());
}
if (!pricePerUnitTxtbox.getText().isEmpty()){
partadd.setPartPrice(Double.parseDouble(pricePerUnitTxtbox.getText()));
}
if (!invTxtbox.getText().isEmpty()){
partadd.setPartInStock(Integer.parseInt(invTxtbox.getText()));
}
if(!minQtyTxtbox.getText().isEmpty()){
partadd.setMin(Integer.parseInt(minQtyTxtbox.getText()));
}
if(!maxQtyTxtbox.getText().isEmpty()){
partadd.setMax(Integer.parseInt(maxQtyTxtbox.getText()));
}
if(!companyNamePrompt.getText().isEmpty()){
((InHouse)partadd).setMachineID(Integer.parseInt(companyNamePrompt.getText()));
}
Inventory.addPart(partadd);
}
else{
Part partaddout = new OutSource(0, "", 0.0, 0,0,0,"");
if(partIDTxtbox.getText().isEmpty()){
partaddout.setPartID(Inventory.getPartIDCount());
}
if (!partNameTxtbox.getText().isEmpty()){
partaddout.setPartName(partNameTxtbox.getText());
}
if (!pricePerUnitTxtbox.getText().isEmpty()){
partaddout.setPartPrice(Double.parseDouble(pricePerUnitTxtbox.getText()));
}
if (!invTxtbox.getText().isEmpty()){
partaddout.setPartInStock(Integer.parseInt(invTxtbox.getText()));
}
if(!minQtyTxtbox.getText().isEmpty()){
partaddout.setMin(Integer.parseInt(minQtyTxtbox.getText()));
}
if(!maxQtyTxtbox.getText().isEmpty()){
partaddout.setMax(Integer.parseInt(maxQtyTxtbox.getText()));
}
if(!companyNamePrompt.getText().isEmpty()){
((OutSource)partaddout).setCompanyName((companyNamePrompt.getText()));
}
Inventory.addPart(partaddout);
}
Parent addPartSave = FXMLLoader.load(getClass().getResource("MainScreen.fxml"));
Scene scene = new Scene(addPartSave);
Stage window = (Stage) ((Node) e.getSource()).getScene().getWindow();
window.setScene(scene);
window.show();
}
public void addPartCancelButton(MouseEvent mouseEvent) throws IOException {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Cancel add part");
alert.setHeaderText("You are about to return to the Main screen!");
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK) {
Parent addPartCancel = FXMLLoader.load(getClass().getResource("MainScreen.fxml"));
Scene scene = new Scene(addPartCancel);
Stage window = (Stage) ((Node) mouseEvent.getSource()).getScene().getWindow();
window.setScene(scene);
window.show();
}
else{
return;
}
}
@Override
public void initialize(URL location, ResourceBundle resources) {
switchfield = new ToggleGroup();
this.addPartInHouse.setToggleGroup(switchfield);
}
}
'''
</details>
# 答案1
**得分**: 0
Here is the translated content:
这不是一个答案,但作为答案比作为评论更容易阅读。
要么 `switchfield` 为 null,要么 `switchfield.getSelectedToggle()` 返回 null。
在第 55 行之前添加以下代码行。
```java
System.out.println(switchfield)
然后运行你的代码。如果显示 switchfield
不为 null,那么将其更改为
System.out.println(switchfield.getSelectedToggle())
然后再次运行代码。
这应该显示 switchfield.getSelectedToggle()
返回 null。
然后,你需要查看 ToggleGroup
类中的 getSelectedToggle()
方法的代码,看看在什么情况下该方法返回 null。
我猜测方法 addPartSaveButton()
可能在你选择单选按钮之前被调用。
英文:
This is not an answer but it is more readable as an answer than as a comment.
Either switchfield
is null or switchfield.getSelectedToggle()
returns null.
Add the following line of code before line 55.
System.out.println(switchfield)
Then run your code. If it shows that switchfield
is not null, then change it to
System.out.println(switchfield.getSelectedToggle())
And run the code again.
That should show that switchfield.getSelectedToggle()
returns null.
Then you need to look at the code for method getSelectedToggle()
in class ToggleGroup
and see when that method returns null.
My guess is that method addPartSaveButton()
is being called before you have selected one of the radio buttons.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论