lundi 27 juillet 2015

JavaFX CheckBoxCell in TableView - Serializing with XStream Exception: Invalid null character in text to output

I try to serialize my ObservableList via XStream. Everything works fine if I DO NOT use a CheckBoxTableCell for my active column (BooleanProperty).

1st the classes:

public abstract class Village {

private Point coordinates;

private SimpleLongProperty id = new SimpleLongProperty(this, "id");
private SimpleStringProperty name = new SimpleStringProperty(this, "name");
private SimpleIntegerProperty xCoord = new SimpleIntegerProperty(this, "xCoord");
private SimpleIntegerProperty yCoord = new SimpleIntegerProperty(this, "yCoord");;
private SimpleIntegerProperty punkte = new SimpleIntegerProperty(this, "punkte");
private SimpleBooleanProperty active = new SimpleBooleanProperty(this, "active");
//Getters and Setters generated with Eclipse, so I dont post them
}

public Class UserVillage extends Village { // at the moment nothing else here}

The static class for serialization:

public class Serializer {

    public static void serialize(Object o, String fileName) {
    XStream xstream = new XStream(new StaxDriver()); // does not require XPP3 library starting with Java 6      
    File file = new File(Constants.PFAD_FUER_SERIALISIERUNG+fileName);
    FileOutputStream outputStream;
    try {
        outputStream = new FileOutputStream(file);
        OutputStreamWriter writer = new OutputStreamWriter(outputStream, Charset.forName("UTF-8"));
        xstream.toXML(o, writer);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

and now the TableController:

public class OwnVillagesListController implements Initializable {

@FXML
private TableView<UserVillage> tableView;

@FXML
private TableColumn<UserVillage, Integer> columnId;
@FXML
private TableColumn<UserVillage, Integer> columnY;
@FXML
private TableColumn<UserVillage, Integer> columnX;
@FXML
private TableColumn<UserVillage, Integer> columnCount;
@FXML
private TableColumn<UserVillage, Boolean> columnActive;
@FXML
private TableColumn<UserVillage, String> columnName;
@FXML 
private Button btnInsertVillages;
@FXML 
private Button btnSave;

private ObservableList<UserVillage> tableData = FXCollections.observableArrayList();

@Override
public void initialize(URL location, ResourceBundle resources) {
    addPropValueFactoryToCells();




    columnCount.setCellValueFactory(column-> new ReadOnlyObjectWrapper<Integer>(tableView.getItems().indexOf(column.getValue())+1));

    columnActive.setCellValueFactory(
            new PropertyValueFactory<UserVillage, Boolean>("active"));
    columnActive.setCellFactory(CheckBoxTableCell.forTableColumn(columnActive));

    columnActive.setEditable(true);

    if(FileUtil.isThereSuchFile(ConstantsFile.FILE_NAME_OWN_VILLAGES))
    {
        tableData.addAll(Deserializer.getOwnVillages());
    }
    else {
        Settings settings = Deserializer.getSettings();
        tableData.addAll(VillageUtil.getAllPlayerVillages(settings));
        tableView.setItems(tableData);

    }
    tableView.setEditable(true);

}

@SuppressWarnings({ "unchecked", "rawtypes"})
private void addPropValueFactoryToCells() {
    columnId.setCellValueFactory(new PropertyValueFactory("id"));
    columnName.setCellValueFactory(new PropertyValueFactory("name"));
    columnX.setCellValueFactory(new PropertyValueFactory("xCoord"));
    columnY.setCellValueFactory(new PropertyValueFactory("yCoord"));
    columnActive.setCellValueFactory(new PropertyValueFactory<UserVillage, Boolean>("active"));

}

@FXML
void btnInsertVillagesListener(ActionEvent event) {
//      Settings settings = Deserializer.getSettings();
    //TODO: logic for automatic import
}

@FXML
void btnSaveListener(ActionEvent event) {
    Serializer.serialize(tableData, ConstantsFile.FILE_NAME_OWN_VILLAGES);
    Logger.info("Saved!");
}
}

Sooo... as mentioned above: if I DO NOT USE the CheckBoxTableCell for my activeColumn the serialization works totally fine. so with a normal view-only column which shows the value of the boolean as a String "true" or "false" everything works. I get this exception (shortform, only LAST 2 blocks):

Caused by: com.ctc.wstx.exc.WstxIOException: Invalid null character in text to output
at com.ctc.wstx.sw.BaseStreamWriter.writeCharacters(BaseStreamWriter.java:511)
at com.thoughtworks.xstream.io.xml.StaxWriter.setValue(StaxWriter.java:158)
... 344 more
Caused by: java.io.IOException: Invalid null character in text to output
at com.ctc.wstx.sw.XmlWriter.throwInvalidChar(XmlWriter.java:538)
at com.ctc.wstx.sw.BufferingXmlWriter.writeCharacters(BufferingXmlWriter.java:531)
at com.ctc.wstx.sw.BaseStreamWriter.writeCharacters(BaseStreamWriter.java:509)
... 345 more

Anyone knows how to fix that? :/ Thanks in advance & greetings from germany, Kevin




Aucun commentaire:

Enregistrer un commentaire