thanks for your time, I've been programming in java for a while but i'm not studing computer science so I don't know lot of things, I'm learning on the way.
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. I'm programming a similiar application but not the same as the tutorial so if I have a problem I can't just look at what's different, however the code is still quite similar and I can compare them, in this case, doing so I couldn't find much of a difference between my "algorhitm" and the one used by the tutorial
The Problem : When loading the data from the xml I get a Null Pointer Exception (see below "loadPersonDataFromFile" method) but I don't know what is causing it
Here I'm reporting some method and classes I think are relevant to the problem, if you need more let me know, and thank you again.
the "savePersonDataToFile" method works fine, here it is:
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();
}
}
and creates a xml which is correct (compared to the one created by the tutorial application)
but the "loadPersonDataFromFile" method does not, here you can see it:
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();
}
}
the exception is given by this command (I added some Console output to find it):
ItemListWrapper wrapper = (ItemListWrapper) um.unmarshal(file);
File is not null and its path is correct. The imported library for Unmarshaller is the correct one (same as the tutorial one).
ItemListWrapper is a class needed to save data into an 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;
}
}
while Item is the object type i'm trying to get from the xml.
Item 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_ I changed the "Exception" into "JAXBException" because i need to handle that, and it says that the NPE is given by this command: String data = dataDue.get(0).getThing();
plus my console logs show that wrapper = null which isn't right (it should cointain my Item objects). So my real problem is that I don't know why wrapper is empty
I was having this problem before (used my log to find it) but then I tought I got another problem because console logs I put after ItemListWrapper wrapper = (ItemListWrapper) um.unmarshal(file);
dindn't show up anymore.
2nd Edit_ NOTE:: this is stacktrace that says ItemListWrapper wrapper = (ItemListWrapper) um.unmarshal(file);
is responsible for the 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
you can try this
unmarshaller.setClassesToBeBound(ItemListWrapper.class);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.