我正在尝试解组我编组的XML文件,但它会返回 “[os.rozgonov.notebook.model.Problem@333c8d07]”当我打印出来时。
我看了这里,但它不起作用Null Pointer Exception in JAXB RI ClassFactory
课堂问题:
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Problem {
private StringProperty problems;
public Problem() {
}
public Problem(String problems) {
this.problems = new SimpleStringProperty(problems);
}
public String getproblems() {
return problems.get();
}
public void setproblems(String problems) {
this.problems.set(problems);
}
public StringProperty problemsProperty() {
return problems;
}
@Override
public String toString(){return getproblems();}
}
打包机:
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javafx.collections.ObservableList;
@XmlRootElement(name = "probl")
public class ProblemListWrapper {
private ObservableList<Problem> problem;
@XmlJavaTypeAdapter(ListProblemAdapter.class)
public ObservableList<Problem> getProblems() {
return problem;
}
public void setProblems(ObservableList<Problem> problems) {
this.problem = problems;
}
}
ListAdapter:
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
public class ListProblemAdapter extends XmlAdapter<ListProblem, ObservableList<Problem>> {
@Override
public ObservableList<Problem> unmarshal(ListProblem v) throws Exception {
ObservableList<Problem> problemData = FXCollections.observableArrayList(v.problem);
return problemData;
}
@Override
public ListProblem marshal(ObservableList<Problem> v) throws Exception {
ListProblem problems = new ListProblem();
v.stream().forEach((item) -> {
problems.problem.add(item);
});
return problems;
}
}
列表:
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
public class ListProblem {
@XmlElement(name = "problem")
List<Problem> problem = new ArrayList<>();
public List<Problem> getproblems() {
return problem;
}
}
主要:
public void loadProblemDataFromFile(File file) {
try {
JAXBContext context = JAXBContext
.newInstance(ProblemListWrapper.class);
Unmarshaller um = context.createUnmarshaller();
ProblemListWrapper wrap = (ProblemListWrapper)um.unmarshal(file);
problemData.clear();
problemData.addAll(wrap.getProblems());
System.out.println(problemData.get(0).toString());
setProblemFilePath(file);
} catch (Exception e) {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText("Could not load data");
alert.setContentText("Could not load data from file:\n" + file.getPath());
alert.showAndWait();
}
}
public void saveProblemDataToFile(File file) {
try {
JAXBContext context = JAXBContext
.newInstance(ProblemListWrapper.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
System.out.println(problemData.get(0).toString());
ProblemListWrapper wrapper = new ProblemListWrapper();
wrapper.setProblems(problemData);
m.marshal(wrapper, file);
setProblemFilePath(file);
} catch (Exception e) { // catches ANY exception
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText("Could not save data");
alert.setContentText("Could not save data to file:\n" + file.getPath());
alert.showAndWait();
}
}
答案 0 :(得分:0)
问题出现在Problem.class
中的默认构造函数中public Problem() {
**this(null);**
那就是