在可观察列表中编组本地日期

时间:2017-07-19 05:47:37

标签: java javafx jaxb marshalling fxml

我有一个可观察的班级清单。使用以下

编组此列表
    public void saveBillsToFile(File file){
    try{    
        JAXBContext context = JAXBContext
                .newInstance(BillListWrapper.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        BillListWrapper wrapper = new BillListWrapper();
        wrapper.setBills(budget.getBills());

        m.marshal(wrapper, file);

        //TODO setPersonFilePath(file);
    }catch(Exception e){
        e.printStackTrace(System.out);
    }

但是当我将BillDate对象属性添加到bill类时,我得到NoSuchMethodException。我发现一篇帖子说我需要创建一个像

这样的XML适配器
public class LocalDateAdapter extends XmlAdapter<String, LocalDate> {
public LocalDate unmarshal(String v) throws Exception {
    return LocalDate.parse(v);
}

public String marshal(LocalDate v) throws Exception {
    return v.toString();
}
}

我不确定在哪里放这个。我假设它与我的包装器有关

@XmlRootElement(name = "bills")
public class BillListWrapper {

private List<Bill> bills;

@XmlElement(name = "bill")
public List<Bill> getBills() {
    return bills;
}

public void setBills(List<Bill> bills) {
    this.bills = bills;
}

Bills课程:

public class Bill{

//Properties
private StringProperty name = new SimpleStringProperty();
private StringProperty description = new SimpleStringProperty();
private IntegerProperty amount = new SimpleIntegerProperty();
private IntegerProperty payable = new SimpleIntegerProperty();
private IntegerProperty allocatedAmount = new SimpleIntegerProperty();
private IntegerProperty owedAmount = new SimpleIntegerProperty();
private IntegerProperty priority = new SimpleIntegerProperty();
private IntegerProperty recurrence = new SimpleIntegerProperty();
private StringProperty payments = new SimpleStringProperty();
//private ObjectProperty<LocalDate> lastPayment = new SimpleObjectProperty<LocalDate>();
int originalAmount;
int originalOwed;


public Bill(){
    name.set("");
    amount.set(0);
    owedAmount.set(0);
    allocatedAmount.set(0);
    description.set("");
    payments.set("");
    originalAmount = 0;
}

public void allocate(int allocation){
    System.out.println(allocation);
    allocatedAmount.set(originalAmount + allocation);
    if(allocation > amount.get()){
        owedAmount.set(originalOwed - allocation + amount.get());
    }else if(allocation < amount.get()){
        owedAmount.set(originalOwed - allocation + amount.get());
    }else if(allocation == amount.get()){
        owedAmount.set(originalOwed);
    }

}

public void pay(){
    if(payable.get() <= allocatedAmount.get()){
        allocatedAmount.set(allocatedAmount.get() - payable.get());
    }else{
        Alert alert = new Alert(AlertType.ERROR);
        alert.setTitle("Error!");
        alert.setHeaderText("Can't Pay");
        alert.setContentText("There is not enough money allocated to pay this.\nChange the value and try again.");

        alert.showAndWait();
    }
}
 //Getters and setters removed

堆栈跟踪:

  

(java:15134):Gtk-WARNING **:无法在module_path中找到主题引擎:&#34; adwaita&#34;,2017年7月19日下午4:12:01   com.sun.xml.internal.bind.v2.ClassFactory create0 INFO:无默认值   在类java.time.LocalDate上找到的构造函数   java.lang.NoSuchMethodException:java.time.LocalDate。()at   java.lang.Class.getConstructor0(Class.java:3082)at   java.lang.Class.getDeclaredConstructor(Class.java:2178)at   com.sun.xml.internal.bind.v2.ClassFactory.create0(ClassFactory.java:89)     在   com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.createInstance(ClassBeanInfoImpl.java:270)at at   com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.createInstance(UnmarshallingContext.java:684)     在   com.sun.xml.internal.bind.v2.runtime.unmarshaller.StructureLoader.startElement(StructureLoader.java:171)     在   com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:559)     在   com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:538)     在   com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:153)     在   com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)     在   com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(AbstractXMLDocumentParser.java:182)     在   com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:351)     在   com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl $ FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2784)     在   com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)     在   com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:112)     在   com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)     在   com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:841)     在   com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:770)     在   com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)     在   com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)     在   com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl $ JAXPSAXParser.parse(SAXParserImpl.java:643)     在   com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:243)     在   com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:214)     在   javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)     在   javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:162)     在   javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:171)     在   javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:189)     在budget.Main.loadBillsFromFile(Main.java:62)at   budget.Main.start(Main.java:39)at   com.sun.javafx.application.LauncherImpl.lambda $ $ launchApplication1 106(LauncherImpl.java:863)     在   com.sun.javafx.application.PlatformImpl.lambda $ runAndWait $ 119(PlatformImpl.java:326)     在   com.sun.javafx.application.PlatformImpl.lambda为$ null $ 117(PlatformImpl.java:295)     在java.security.AccessController.doPrivileged(Native Method)at   com.sun.javafx.application.PlatformImpl.lambda $ runLater $ 118(PlatformImpl.java:294)     在   com.sun.glass.ui.InvokeLaterDispatcher $ Future.run(InvokeLaterDispatcher.java:95)     at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)at   com.sun.glass.ui.gtk.GtkApplication.lambda为$ null $ 450(GtkApplication.java:139)     在java.lang.Thread.run(Thread.java:748)

     

Application start方法中的异常   java.lang.reflect.InvocationTargetException at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)at   com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)     在   com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)at   sun.launcher.LauncherHelper $ FXHelper.main(LauncherHelper.java:767)   引起:java.lang.RuntimeException:Application start中的异常   方法   com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)     在   com.sun.javafx.application.LauncherImpl.lambda $ launchApplication $ 99(LauncherImpl.java:182)     在java.lang.Thread.run(Thread.java:748)引起:   java.lang.NoSuchMethodError:java.time.LocalDate。()at   com.sun.xml.internal.bind.v2.ClassFactory.create0(ClassFactory.java:96)     在   com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.createInstance(ClassBeanInfoImpl.java:270)at at   com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.createInstance(UnmarshallingContext.java:684)     在   com.sun.xml.internal.bind.v2.runtime.unmarshaller.StructureLoader.startElement(StructureLoader.java:171)     在   com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:559)     在   com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:538)     在   com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:153)     在   com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)     在   com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(AbstractXMLDocumentParser.java:182)     在   com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:351)     在   com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl $ FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2784)     在   com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)     在   com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:112)     在   com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)     在   com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:841)     在   com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:770)     在   com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)     在   com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)     在   com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl $ JAXPSAXParser.parse(SAXParserImpl.java:643)     在   com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:243)     在   com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:214)     在   javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)     在   javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:162)     在   javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:171)     在   javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:189)     在budget.Main.loadBillsFromFile(Main.java:62)at   budget.Main.start(Main.java:39)at   com.sun.javafx.application.LauncherImpl.lambda $ $ launchApplication1 106(LauncherImpl.java:863)     在   com.sun.javafx.application.PlatformImpl.lambda $ runAndWait $ 119(PlatformImpl.java:326)     在   com.sun.javafx.application.PlatformImpl.lambda为$ null $ 117(PlatformImpl.java:295)     在java.security.AccessController.doPrivileged(Native Method)at   com.sun.javafx.application.PlatformImpl.lambda $ runLater $ 118(PlatformImpl.java:294)     在   com.sun.glass.ui.InvokeLaterDispatcher $ Future.run(InvokeLaterDispatcher.java:95)     at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)at   com.sun.glass.ui.gtk.GtkApplication.lambda为$ null $ 450(GtkApplication.java:139)     ... 1更多引起:java.lang.NoSuchMethodException:   java.time.LocalDate。()at   java.lang.Class.getConstructor0(Class.java:3082)at   java.lang.Class.getDeclaredConstructor(Class.java:2178)at   com.sun.xml.internal.bind.v2.ClassFactory.create0(ClassFactory.java:89)     ... 35更多异常运行应用程序预算。主要

0 个答案:

没有答案