-
Notifications
You must be signed in to change notification settings - Fork 115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problem deserializing ObservableObjects #42
Comments
If you notice near the end of the stack trace, the last 'Caused by:' entry, it says a NullPointerException occurred. It occurred inside the ObservableListWrapper.size() method. Taking a look at that method, you will see that all it does is call backingList.size(). The backingList member variable is obviously null, and calling size() on it causes the NPE. This happens because during instantiation of the ObservableListWrapper, the backingList member variable is not getting set. json-io allows you to assign your own 'instantiator' class for situations like this. Use the JsonReader.assignInstantiator() method to assign your own JsonReader.ClassFactory class to be called when a new instance of ObservableListWrapper is needed:
This will associate a JsonReader.ClassFactory instance to be called which will create the ObservableListWrapper when needed.
When the JsonReader encounters an ObservableListWrapper that needs to be created, it will call your factory class to get a new one. Since you are in charge of writing that class, you can properly create a new ObservalListWrapper and return it. JsonReader does a lot of work to attempt to construct any class it encounters, including calling all constructors, private constructors, even using sun.misc.Unsafe if available. However, some classes still require their constructors to be called. This is why JsonReader has the ability to allow you to install your own 'constructors' as needed. |
Ok that is nice, I see now the power of json-io with that feature. I readed twice your explanation and I search in the api but I can't figure how to "properly create an new ObservableListWrapper and return it here", given a Class as parameter. I checked the code of JsonReader's CollectionFactory class and I infered that the naive approach would be something like public class OberservableListWrapperCreator implements ClassFactory {
@Override
public Object newInstance(Class c) {
if(ObservableListWrapper.class.isAssignableFrom(c))
return new ObservableListWrapper(null);
else
throw new JsonIoException("CollectionFactory handed Class for which it was not expecting: " + c.getName());
}
} But obviously isn't. Sorry for my skills, can you tell me more about how I have to implement that? Thanks a lot |
First, you do not need an 'isAssignableFrom()' check. Your code will only be I am not familiar with this particular (ObservableListWrapper) class, but I am sure there is a On Sun, Jun 21, 2015 at 7:18 PM, Otto notifications@github.com wrote:
|
Thank you a lot, I just did it. |
I'm coding a JavaFX application and I want to serialize/deserialize a list of objects that are observable cause they are used in a
TableView
. I tested that the serialization/deserialization with json-io of aList
works well, but when I try to do the same thing with anObservableList
I getJsonIoException
when callingreadObject()
from theJsonReader
instanceThis is the failure trace from JUnit
Any ideas about the reason?
The text was updated successfully, but these errors were encountered: