Coder Social home page Coder Social logo

Comments (4)

jdereg avatar jdereg commented on August 26, 2024

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:

JsonReader.assignInstantiator(ObservableListWrapper.class, myObservableListWrapperCreator);

This will associate a JsonReader.ClassFactory instance to be called which will create the ObservableListWrapper when needed.

public static class MyObservableListWrapperCreator implements JsonReader.ClassFactory
{
    Object newInstance(Class c)
    {
        // properly create an new ObservableListWrapper and return it here
    }
}

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.

from json-io.

octaviospain avatar octaviospain commented on August 26, 2024

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

from json-io.

jdereg avatar jdereg commented on August 26, 2024

First, you do not need an 'isAssignableFrom()' check. Your code will only be
called with a ObservableListWrapper.class. Pass whatever you need to
your ObservableListWrapperCreator in it's constructor that you would need
to create the class properly. Currently, your class does not have any constructor, so your 'newInstance(Class c)' method has no 'help' to create the class.

I am not familiar with this particular (ObservableListWrapper) class, but I am sure there is a
'normal' way to create an instance of ObservableListWrapper. Create the
instance in this method this 'normal' way [look at the JDK code if necessary], and then return it. If you need some external instances, those can be passed through your [missing] constructor, and then
used in the newInstance() method.

On Sun, Jun 21, 2015 at 7:18 PM, Otto [email protected] wrote:

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


Reply to this email directly or view it on GitHub
#42 (comment).

from json-io.

octaviospain avatar octaviospain commented on August 26, 2024

Thank you a lot, I just did it.

from json-io.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.