2016-06-24 2 views
1
public class ControllerMain implements Initializable { 
private int id; 
private String nameCompany; 
private int phone; 
private String address; 
private String other; 
static ObservableList<UserData> data = FXCollections.observableArrayList(); 

@FXML 
private Button btnAdd; 
@FXML 
public TableView<UserData> table = new TableView<>(); 
@FXML 
private TableColumn<UserData, String> column1; 
@FXML 
private TableColumn<UserData, Integer> column2; 
@FXML 
private TableColumn<UserData, String> column3; 
@FXML 
private TableColumn<UserData, String> column4; 
@FXML 
private TableColumn<UserData, Integer> column5; 

@Override 
public void initialize(URL location, ResourceBundle resources) { 
    String companyName = "companyName"; 
    column1.setCellValueFactory(new PropertyValueFactory<UserData, String>(companyName)); 
    String phone = "phone"; 
    column2.setCellValueFactory(new PropertyValueFactory<UserData, Integer>(phone)); 
    String address = "address"; 
    column3.setCellValueFactory(new PropertyValueFactory<UserData, String>(address)); 
    String other = "other"; 
    column4.setCellValueFactory(new PropertyValueFactory<UserData, String>(other)); 
    String id = "id"; 
    column5.setCellValueFactory(new PropertyValueFactory<UserData, Integer>(id)); 
    column5.setVisible(false); 
    loadDatabaseData(); 
} 

@FXML 
private void openAddForm() { 
    try { 
     MainApp.showAddForm(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

@FXML 
private void deleteData() { 
    try (Connection con = new DBConnect().getConnected(); 
     PreparedStatement prep = con.prepareStatement("DELETE FROM job.job WHERE job.id = ?")) { 
     UserData selectedItem = table.getSelectionModel().getSelectedItem(); 
     prep.setInt(1, selectedItem.idProperty().getValue()); 
     prep.execute(); 
     data.remove(selectedItem); 
    } catch (Exception e) { 
     System.err.println("Ошибка удаления: " + e.getMessage()); 
    } 
} 

@FXML 
private void openUpdateForm() { 
    try { 
     UserData selectedItem = table.getSelectionModel().getSelectedItem(); 
     setId(selectedItem.idProperty().getValue()); 
     setNameCompany(selectedItem.companyNameProperty().getValue()); 
     setPhone(selectedItem.phoneProperty().getValue()); 
     setAddress(selectedItem.addressProperty().getValue()); 
     setOther(selectedItem.otherProperty().getValue()); 
     MainApp.showUpdateForm(); 
    } catch (Exception e) { 
     System.err.println("Ошибка открытия формы редактивания: " + e.getMessage()); 
     e.printStackTrace(); 
    } 
} 

void loadDatabaseData() { 
    try (Connection con = new DBConnect().getConnected(); 
     PreparedStatement preparedStatement = con.prepareStatement("SELECT * FROM job.job"); 
     ResultSet resultSet = preparedStatement.executeQuery()) { 
     data.clear(); 
     while (resultSet.next()) { 
      data.add(new UserData(
        resultSet.getInt("id"), 
        resultSet.getString("company_name"), 
        resultSet.getInt("phone"), 
        resultSet.getString("address"), 
        resultSet.getString("other") 
      )); 
      table.setItems(data); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
     System.out.println("Error on Building Data"); 
    } 
} 


private void setId(int id) { 
    this.id = id; 
} 

String getNameCompany() { 
    return nameCompany; 
} 

private void setNameCompany(String nameCompany) { 
    this.nameCompany = nameCompany; 
} 

int getPhone() { 
    return phone; 
} 

private void setPhone(int phone) { 
    this.phone = phone; 
} 

String getAddress() { 
    return address; 
} 

private void setAddress(String address) { 
    this.address = address; 
} 

String getOther() { 
    return other; 
} 

private void setOther(String other) { 
    this.other = other; 
}} 

및 기타 클래스게터 방법은

public class ControllerUpdateData implements Initializable { 
@FXML 
private TextField txt2; 
@FXML 
private TextField txt3; 
@FXML 
private TextField txt4; 
@FXML 
private TextField txt1; 

@FXML 
private void updateData() { 
    ControllerMain controllerMain = new ControllerMain(); 
    try (Connection con = new DBConnect().getConnected(); 
     PreparedStatement prep = con.prepareStatement("UPDATE job.job SET company_name=?,phone=?,address=?,other=? WHERE job.id=?;", Statement.RETURN_GENERATED_KEYS)) { 
     prep.setString(1, txt1.getText()); 
     prep.setInt(2, Integer.parseInt(txt2.getText())); 
     prep.setString(3, txt3.getText()); 
     prep.setString(4, txt4.getText()); 
     prep.setInt(5, 1); 
     prep.execute(); 
     txt1.clear(); 
     txt2.clear(); 
     txt3.clear(); 
     txt4.clear(); 
     controllerMain.loadDatabaseData(); 
    } catch (IllegalAccessException | InstantiationException | ClassNotFoundException | SQLException e) { 
     System.err.println("Error: " + e.getMessage()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 


@Override 
public void initialize(URL location, ResourceBundle resources) { 
    ControllerMain controllerMain = new ControllerMain(); // get Null 
    txt1.setText(controllerMain.getNameCompany()); // get Null 
    txt2.setText(String.valueOf(controllerMain.getPhone())); // get Null 
    txt3.setText(controllerMain.getAddress()); // get Null 
    txt4.setText(controllerMain.getOther()); // 
}} 

나는 방법에 getter 및 setter를 배치 한 다음 메인 메소드에서 해당 메소드를 호출, 나는에도 불구하고 null 값을 얻을 값을 다른 것으로 설정하십시오! 또한 컴파일러에서 오류가 발생하지 않으므로 어딘가에 논리적 오류라고 확신하지만 해결할 수는 없습니다. 당신은 ControllerUpdateDatainitialize이를 교체해야

+0

그래서 왜 txt1.setText (controllerMain.getNameCompany()); // get Null'이 줄은 NullPointerException을 던집니다? – DVarga

+0

System.out.println (controllerMain.getNameCompany()); // 인쇄 null –

+0

읽지 못했습니다 ... [mcve]를 참조하십시오. –

답변

0

...

ControllerMain controllerMain = new ControllerMain(); // get Null 

...

FXMLLoader loader = new FXMLLoader(getClass().getResource("... FXML_of_ControllerMain ...")); 
ControllerMain controllerMain = (ControllerMain) loader.getController(); 

문제는 당신은 사용 ControllerMain의 인스턴스를 생성하는 경우는 기본 생성자를 사용하면 initialize 메서드가 호출되지 않으므로 클래스 멤버 중 초기화되지 않습니다.

당신은 FXML 파일을로드 할 FXMLLoader를 사용해야하고, loadFXMLLoader는 관련 컨트롤러 클래스의 인스턴스를 생성하고 해당 인스턴스에 initialize 메소드를 호출합니다. 이 개체는 getController 메서드를 사용하여 검색 할 수 있습니다.

컨트롤러에서 FXML 파일을로드하는 방법은 really detailed answer입니다.