尝试过滤从文本字段中搜索表格时遇到麻烦

时间:2018-11-10 08:10:53

标签: java sql javafx filter scenebuilder

 Caused by: java.lang.NullPointerException
    at javafx.collections.transformation.TransformationList.<init>(TransformationList.java:65)
    at javafx.collections.transformation.FilteredList.<init>(FilteredList.java:66)
    at sample.Cust_Controller.srch(Cust_Controller.java:554)
    at sample.Cust_Controller.initialize(Cust_Controller.java:603)

我的文本字段称为搜索

private void srch(){

    columnFname.setCellValueFactory(cellData -> cellData.getValue().FirstNameProperty());
    columnLname.setCellValueFactory(cellData -> cellData.getValue().LastNameProperty());

    FilteredList<CustomerDetails> filteredData = new FilteredList<>(data, p -> true);

    search.textProperty().addListener((observable, oldValue, newValue) -> {
        filteredData.setPredicate((Predicate<? super CustomerDetails>) (CustomerDetails csd) -> {
            // If filter text is empty, display all persons.
            if (newValue == null || newValue.isEmpty()) {
                return true;
            }

            // Compare first name and last name of every person with filter text.
            String lowerCaseFilter = newValue.toLowerCase();

            if (csd.getFirstName().toLowerCase().contains(lowerCaseFilter)) {
                return true; // Filter matches first name.
            } else if (csd.getLastName().toLowerCase().contains(lowerCaseFilter)) {
                return true; // Filter matches last name.
            }
            return false; // Does not match.
        });
    });
    // 3. Wrap the FilteredList in a SortedList.
    SortedList<CustomerDetails> sortedData = new SortedList<>(filteredData);

    // 4. Bind the SortedList comparator to the TableView comparator.
    sortedData.comparatorProperty().bind(tableCustomer.comparatorProperty());

    // 5. Add sorted (and filtered) data to the table.
    tableCustomer.setItems(sortedData);

}

我的客户详细信息

public class CustomerDetails {
    private final StringProperty custID;
    private final StringProperty FirstName;
    private final StringProperty LastName;
    private final StringProperty Organization;
    private final StringProperty Phone;
    private final StringProperty Email;
    private final StringProperty CusTypeID;
    private final StringProperty CusTypeName;



    public CustomerDetails(String custID, String FirstName, String LastName, String Organization, String Phone, String Email,String CusTypeID, String CusTypeName) {
        this.custID = new SimpleStringProperty(custID);
        this.FirstName = new SimpleStringProperty(FirstName);
        this.LastName = new SimpleStringProperty(LastName);
        this.Organization = new SimpleStringProperty(Organization);
        this.Phone = new SimpleStringProperty(Phone);
        this.Email = new SimpleStringProperty(Email);
        this.CusTypeID = new SimpleStringProperty(CusTypeID);
        this.CusTypeName = new SimpleStringProperty(CusTypeName);
    }

    public String getcustID() {
        return (String)this.custID.get();
    }

    public String getFirstName() {
        return (String)this.FirstName.get();
    }

    public String getLastName() {
        return (String)this.LastName.get();
    }

    public String getOrganization() {
        return (String)this.Organization.get();
    }

    public String getPhone() {
        return (String)this.Phone.get();
    }

    public String getEmail() {
        return (String)this.Email.get();
    }

    public String getCustTypeID() { return (String)this.CusTypeID.get();}

    public String getCustTypeName() { return (String)this.CusTypeName.get();}

    public void setcustID(String value) {
        this.custID.set(value);
    }

    public void setFirstName(String value) {
        this.FirstName.set(value);
    }

    public void setLastName(String value) {
        this.LastName.set(value);
    }

    public void setOrganization(String value) {
        this.Organization.set(value);
    }

    public void setPhone(String value) {
        this.Phone.set(value);
    }

    public void setEmail(String value) {
        this.Email.set(value);
    }

    public void setCusTypeID(String value) { this.CusTypeID.set(value);}

    public void setCusTypeName(String value) { this.CusTypeName.set(value);}

    public StringProperty custIDProperty() {
        return this.custID;
    }

    public StringProperty FirstNameProperty() {
        return this.FirstName;
    }

    public StringProperty LastNameProperty() {
        return this.LastName;
    }

    public StringProperty FirstOrganizationProperty() {
        return this.Organization;
    }

    public StringProperty PhoneProperty() {
        return this.Phone;
    }

    public StringProperty EmailProperty() {
        return this.Email;
    }

    public StringProperty CusTypeID() {return this.CusTypeID;}

    public StringProperty CusTypeName() {return this.CusTypeName;}
    }

0 个答案:

没有答案