[英]JAVA Xml Unmarshaller Null Pointer Exception (+ JavaFx, but I don't think it is relevant to my problem)
感謝您的寶貴時間,我已經在 java 編程了一段時間,但我不學習計算機科學,所以我不知道很多東西,我正在學習。
Context : I'm following a tutorial (mainly about javafx: https://code.makery.ch/it/library/javafx-tutorial/part5/ ) to learn how to save and load data to/from an xml file. 我正在編寫一個類似的應用程序,但與教程不同,所以如果我有問題,我不能只看有什么不同,但是代碼仍然非常相似,我可以比較它們,在這種情況下,這樣做我在我的“算法”和本教程使用的算法之間找不到太大的區別
問題:當從 xml 加載數據時,我得到一個 Null 指針異常(見下文“loadPersonDataFromFile”方法),但我不知道是什么原因造成的
在這里,我報告了一些我認為與問題相關的方法和類,如果您需要更多信息,請告訴我,再次感謝您。
“savePersonDataToFile”方法工作正常,這里是:
public void savePersonDataToFile(File file) {
try {
JAXBContext context = JAXBContext
.newInstance(ItemListWrapper.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
// Wrapping our person data.
ItemListWrapper wrapper = new ItemListWrapper();
wrapper.setPersons(itemData);
// Marshalling and saving XML to the file.
m.marshal(wrapper, file);
// Save the file path to the registry.
setPersonFilePath(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();
}
}
並創建一個正確的 xml(與教程應用程序創建的相比)
但是“loadPersonDataFromFile”方法沒有,在這里你可以看到它:
public void loadPersonDataFromFile(File file) {
try {
System.out.print("1\n");
JAXBContext context = JAXBContext
.newInstance(ItemListWrapper.class);
Unmarshaller um = context.createUnmarshaller();
System.out.print("2\n");
System.out.print(String.valueOf(file.getPath()) + "\n");
ItemListWrapper wrapper = (ItemListWrapper) um.unmarshal(file);
System.out.print("3\n");
List<Item> dataDue = wrapper.getItems();
System.out.print("3.5\n");
System.out.print(String.valueOf(dataDue) + "\n");
String data = dataDue.get(0).getThing();
System.out.print(data);
itemData.clear();
itemData.addAll(wrapper.getItems());
System.out.print("4\n");
// Save the file path to the registry.
setPersonFilePath(file);
System.out.print("5\n");
} catch (Exception e) { // catches ANY exception
System.out.print(e + "\n");
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();
}
}
這個命令給出了異常(我添加了一些控制台 output 來找到它):
ItemListWrapper wrapper = (ItemListWrapper) um.unmarshal(file);
文件不是 null 並且它的路徑是正確的。 為 Unmarshaller 導入的庫是正確的(與教程中的相同)。
ItemListWrapper 是一個 class 需要將數據保存到 xml 中:
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "items")
public class ItemListWrapper {
private List<Item> items;
@XmlElement(name = "item")
public List<Item> getItems() {
return items;
}
public void setPersons(List<Item> items) {
this.items = items;
}
}
而項目是 object 類型,我試圖從 xml 獲得。
項目 Class:
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Item
{
private final StringProperty status;
private final StringProperty thing;
public Item() {
this(null,null);
}
public Item(String status, String thing) {
this.status = new SimpleStringProperty(status);
this.thing = new SimpleStringProperty(thing);
}
public String getStatus()
{
return status.get();
}
public void setStatus(String status) {
this.status.set(status);
}
public StringProperty statusProperty()
{
return status;
}
public String getThing()
{
return thing.get();
}
public void setThing(String thing) {
this.thing.set(thing);
}
public StringProperty thingProperty() {
return thing;
}
}
1 Edit_我將“異常”更改為“JAXBException”,因為我需要處理它,它說NPE是由這個命令給出的: String data = dataDue.get(0).getThing();
加上我的控制台日志顯示 wrapper = null 這是不正確的(它應該包含我的 Item 對象)。 所以我真正的問題是我不知道為什么 wrapper 是空的
我之前遇到過這個問題(使用我的日志找到它)但后來我又遇到了另一個問題,因為控制台日志我放在ItemListWrapper wrapper = (ItemListWrapper) um.unmarshal(file);
之后不再出現。
第二次編輯_注意:這是堆棧跟蹤,上面寫着ItemListWrapper wrapper = (ItemListWrapper) um.unmarshal(file);
負責 NPE
Exception in Application start method java.lang.reflect.InvocationTargetException 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 com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328) 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.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:873) Caused by: java.lang.RuntimeException: Exception in Application start method at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182) at java.lang.Thread.run(Thread.java:748) Caused by: 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:610) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1784) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2969) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:605) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:113) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:507) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:867) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:796) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:142) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1216) at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:644) 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 application.Main.loadPersonDataFromFile(Main.java:196) at application.Main.initRoot(Main.java:83) at application.Main.start(Main.java:56) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863) at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326) at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:187)... 1 more Exception running application application.Main
你可以試試這個
unmarshaller.setClassesToBeBound(ItemListWrapper.class);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.