JavaFX无法在填充comboBox后加载场景

时间:2019-04-28 03:27:44

标签: javafx combobox fxml scenebuilder observablelist

我正在尝试创建一个可以在borderPane上动态加载场景的GUI(用户可以通过单击按钮来更改场景)。 然后我想添加一个comboBox,其中装有来自observablelist的某些选项 但是在填充comboBox之后,程序无法加载场景

尝试加载场景时出现此错误

Caused by: java.lang.NullPointerException
    at prototipegui1.FXMLDocumentController.cekSource(FXMLDocumentController.java:87)
    at prototipegui1.FXMLDocumentController.initialize(FXMLDocumentController.java:39)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    ... 50 more

在我添加它之前可以正常工作

sourceCbx.setItems(sourceList);
package prototipegui1;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.ComboBox;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import com.fazecast.jSerialComm.SerialPort;

public class FXMLDocumentController implements Initializable {

    @FXML
    private AnchorPane mainPane;
    @FXML
    private BorderPane fillPane;
    @FXML 
    private ComboBox<String> sourceCbx;

    private ObservableList<String> sourceList = FXCollections.observableArrayList();


    @Override
    public void initialize(URL url, ResourceBundle rb){
        cekSource();
    }
    @FXML
    private void Close(MouseEvent event) {
        Stage stage = (Stage) mainPane.getScene().getWindow();
        stage.close();
    }
    @FXML
    private void Central(MouseEvent event) {
        loadPane("FXMLcentral");
    }
    @FXML
    private void Temperature(MouseEvent event) {
        loadPane("FXMLtemperature");
    }
    @FXML
    private void Proximity(MouseEvent event) {
        loadPane("FXMLproximity");
    }
    @FXML
    private void Electricity(MouseEvent event) {
        loadPane("FXMLelectricity");
    }
    @FXML
    private void Humidity(MouseEvent event) {
        loadPane("FXMLhumidity");
    }
    @FXML
    private void LoginRecord(MouseEvent event) {
        loadPane("FXMLrecord");
    }
    private void loadPane(String pane){
        Parent root = null;
        try{
            root=FXMLLoader.load(getClass().getResource(pane+".fxml"));
        }catch(IOException ex){
            Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex );
        }
        fillPane.setCenter(root);
    }
    private void cekSource(){//masih mengganggu fungsi tombol lain
        SerialPort[] portNames = SerialPort.getCommPorts();
            for(int i = 0; i < portNames.length; i++){
                sourceList.add(portNames[i].getSystemPortName());
            }  
        sourceCbx.setItems(sourceList);
    }


}

选中此What is a NullPointerException, and how do I fix it?后 我得到了解决方案

If(sourceCbx!=null){
sourceCbx.setItems(sourceList);
}

0 个答案:

没有答案