2017-10-07 3 views
-1

JavaFxアプリケーション内のアンケートから質問を保存してロードします。 JAXBを使ってクラスを.xmlファイルに保存するメソッドは正常に動作しますが、うまくいけばunmallallerを動作させることができません。ロードする.xmlを選択するたびに、何も起こりません。エラーはありません。異なる質問の値は、保存された質問に変更されません。JAXB:XMLアンマーシャラーは何もロードしていません

私はステップごとにガイドthisに従っていますが、見つからない部分があります。

ロード・メソッド:

public void loadQuestions(File file) throws Exception{ 

    JAXBContext context = JAXBContext.newInstance(QuestionListWrapper.class); 
    Unmarshaller um = context.createUnmarshaller(); 

    QuestionListWrapper wrapper = (QuestionListWrapper) um.unmarshal(file); 

    survey.getQuestions().clear(); 
    survey.getQuestions().addAll(wrapper.getQuestions()); 

} 

@FXML private Button bLoad; 

@FXML 
private void handleLoad() throws Exception{ 

    Stage stage = (Stage) bLoad.getScene().getWindow(); 

    FileChooser fileChooser = new FileChooser(); 
    FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("XML files (*.xml)", "*.xml"); 
    fileChooser.getExtensionFilters().add(extFilter); 

    File file = fileChooser.showOpenDialog(stage); 

    if (file != null){ 
     loadQuestions(file); 
    } 

} 

質問クラス:

package survey.model; 

import javafx.beans.property.*; 

public class Question { 

private StringProperty text; 
private BooleanProperty answer; 
private StringProperty comment; 
private StringProperty information; 

public Question() { 
    text = new SimpleStringProperty(""); 
    answer = new SimpleBooleanProperty(false); 
    comment = new SimpleStringProperty(""); 
    information = new SimpleStringProperty(""); 

} 

public String getInformation() { 
    return information.get(); 
} 

public StringProperty informationProperty() { 
    return information; 
} 

public void setInformation(String information) { 
    this.information.set(information); 
} 

public String getComment() { return comment.get(); } 

public StringProperty commentProperty() { 
    return comment; 
} 

public void setComment(String comment) { 
    this.comment.set(comment); 
} 


public Question(String text) { 
    setText(text); 
} 


public boolean isAnswer() { 
    return answer.get(); 
} 

public BooleanProperty answerProperty() { 
    return answer; 
} 

public void setAnswer(boolean answer) { 
    this.answer.set(answer); 
} 


public String getText() { 
    return text.get(); 
} 

public StringProperty textProperty() { 
    return text; 
} 

public void setText(String text) { 
    this.text.set(text); 
} 
} 

調査クラス:

package survey.model; 

import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 

public class Survey { 

private ObservableList<Question> questions = FXCollections.observableArrayList(); 

public ObservableList<Question> getQuestions() { 
    return questions; 
} 

} 

QuestionListWrapper:

package survey.model; 

import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 
import java.util.List; 

@XmlRootElement(name = "questions") 
public class QuestionListWrapper { 

private List<Question> questions; 

@XmlElement(name = "question") 
public List<Question> getQuestions(){ 
    return questions; 
} 

public void setQuestions(List<Question> questions){ 
    this.questions = questions; 
} 

} 

XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<questions> 
<question> 
    <answer>true</answer> 
    <comment></comment> 
    <information>asfd</information> 
    <text>Der Stelleninhaber wird in verschiedenen Tätigkeitsbereichen eingesetzt (z.B. durch ein vorhandenes Rotationssystem).</text> 
</question> 
<question> 
    <answer>true</answer> 
    <comment></comment> 
    <information>sdasd</information> 
    <text>Die Zuteilung und Erweiterung von Tätigkeitsbereichen erfolgt unter Berücksichtigung der Fähigkeiten, Kenntnisse und Wünsche des Stelleninhabers.</text> 
</question> 
<question> 
    <answer>false</answer> 
    <comment>asdasd</comment> 
    <information>sadsad</information> 
    <text>Gleiche Teiltätigkeiten wiederholen sich so häufig, dass die Arbeit monoton und eintönig erscheint.</text> 
</question> 
<question> 
    <answer>false</answer> 
    <comment></comment> 
    <information>asdsad</information> 
    <text>Die Arbeit wird hauptsächlich in einer Körperhaltung ausgeführt</text> 
</question> 
</questions> 

誰が欠けているものか、間違っているかもしれないものを知っていますか?前もって感謝します!

EDIT: 私はSurveyQuestionListWrapperのメソッドが同じ名前を持っているので、多分それは正しくロードされないと思ったので、私は、QuestionListWrapperからgetQuestions()の名前を変更しました。

java.lang.NullPointerException 
at com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(Lister.java:289) 
at com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(Lister.java:253) 
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Scope.add(Scope.java:106) 
at com.sun.xml.internal.bind.v2.runtime.property.ArrayERProperty$ReceiverImpl.receive(ArrayERProperty.java:198) 
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.endElement(UnmarshallingContext.java:597) 
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.endElement(SAXConnector.java:165) 
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:609) 
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1782) 
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2967) 
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602) 
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:112) 
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505) 
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:841) 
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:770) 
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141) 
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213) 
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643) 
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:243) 
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:214) 
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157) 
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:162) 
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:171) 
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:189) 
at survey.view.moduleAabController.loadQuestions(moduleAabController.java:306) 
at survey.view.moduleAabController.handleLoad(moduleAabController.java:325) 
at survey.view.moduleAabController.handleButtonAction(moduleAabController.java:258) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71) 
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275) 
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769) 
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657) 
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49) 
at javafx.event.Event.fireEvent(Event.java:198) 
at javafx.scene.Node.fireEvent(Node.java:8413) 
at javafx.scene.control.Button.fire(Button.java:185) 
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182) 
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96) 
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89) 
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218) 
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) 
at javafx.event.Event.fireEvent(Event.java:198) 
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757) 
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485) 
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762) 
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494) 
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381) 
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295) 
at java.security.AccessController.doPrivileged(Native Method) 
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417) 
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389) 
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416) 
at com.sun.glass.ui.View.handleMouseEvent(View.java:555) 
at com.sun.glass.ui.View.notifyMouse(View.java:937) 
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) 
at java.lang.Thread.run(Thread.java:745) 
+0

「何も起こらない」?まあ、明らかに何かが起こる。何かエラーがありますか?あなたのXMLと 'QuestionListWrapper'はどのように見えますか? – lexicore

+0

@lexicore質問を更新しました。私は何の誤りもありません。 'survey 'の質問の値は、.xmlをアプリケーションにロードするときに変更されません。 – jeypeu

+0

@lexicore可能性のあるエラーソースが見つかりました(EDITを参照)。 – jeypeu

答えて

0

@XmlElementWrapper@XmlElement(name = "question")前には動作するはずの追加:その後、私は次のエラーを取得します。

関連する問題