You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@isis.apache.org by Jeremy Gurr <je...@infusionsoft.com> on 2013/09/10 20:08:02 UTC

NullPointerException when trying to persist a domain entity

I'm getting this when trying to run a test I'm making:

java.lang.NullPointerException
at org.apache.isis.core.metamodel.services.container.DomainObjectContainerDefault.persist(DomainObjectContainerDefault.java:243)
at org.apache.isis.applib.AbstractContainedObject.persist(AbstractContainedObject.java:278)

These line numbers are from a fresh pull performed this morning, so it's likely they will still be correct if you are looking at the head of the master branch.

I'm sure I've misconfigured something, but I'm trying to understand what's happening here so I can fix it. It would also be nice if nulls were checked for in this part of code and a more useful error message thrown. Here is the code throwing the exception:

    @Override
    public void persist(final Object transientObject) {
        final ObjectAdapter adapter = getAdapterManager().getAdapterFor(transientObject);
        // TODO check aggregation is supported
        if (adapter.isParented()) {
            return;
        }

In this case, the adapter is null, and so the isParented method is called on a null object, throwing the exception.

The transientObject being passed in here is an instance of my model class named ServiceClass. The service class which contains methods to create new instances and perform various searches is called ServiceClassService (analogous to TodoItems). I stepped through this code and can see the map it is looking in for the getAdapterFor method contains the services I registered, and so it could look up ServiceClassService, but not ServiceClass. But I'm trying to persist a data entity (ServiceClass), so I'm not sure what's supposed to be happening here.

I have a class which extends AbstractFactoryAndRepository which of course has a persist method. I call that method to persist a newly created transient entity, but this exception is thrown. Persist seems to be the method I'm looking for, but why does it look to the service list to find which adapter to use to persist the entity? And how do I either get the correct service registered, or change the ServiceClassService so that it recognizes the ServiceClass entity as something it can work with? Sorry this probably doesn't make much sense, because I don't quite understand what the persistence code is trying to do here. Any help would be greatly appreciated :)

-- Jeremy

Re: NullPointerException when trying to persist a domain entity

Posted by Dan Haywood <da...@haywood-associates.co.uk>.
Further to this, I've now changed the framework [1] so that simply newing
up the object will work (rather than requiring newTransientInstance())

There are some (relatively minor) implications to this, in the ticket.

I've also added Javadoc so that this is all more easily discoverable.

Any questions/issues, let me know

Cheers
Dan


[1] https://issues.apache.org/jira/browse/ISIS-528


On 10 September 2013 20:06, Jeremy Gurr <je...@infusionsoft.com> wrote:

> Yeah, using newTransientInstance to obtain the entity vs. newing one up
> manually worked. Thanks.
>
> Automatic recovery would be nice to make it easier for newbies to get up
> to speed with the framework. At a minimum, the javadoc for the persist
> method should indicate that you can't just persist any data entity, but
> that the entity must be obtained from the newTransientInstance method.
>
>
> ________________________________________
> From: Dan Haywood [dan@haywood-associates.co.uk]
> Sent: Tuesday, September 10, 2013 11:51 AM
> To: users
> Subject: Re: NullPointerException when trying to persist a domain entity
>
> How did you instantiate the transient object?  Did you just new it up, or
> did you use container.newTransientInstance()?
>
> I'm *hoping* the former.  The right thing to do is the latter.
>
> If the above doesn't sort it out, then could you restate the problem but
> using some different class names.  I got a bit confused with your
> description, which class is the persistable entity (cf Customer), which
> class is the domain service (cf CustomerRepo).
>
> I agree that we should improve the error messages here.  We might even try
> to automatically recover from the situation (by creating the adapter lazily
> if not present) if we are passed a pojo that was created "outside" of the
> container.  Off the top of my head, I can't see why that won't work (but I
> might be forgetting something).
>
> Cheers
> Dan
>

Re: NullPointerException when trying to persist a domain entity

Posted by Dan Haywood <da...@haywood-associates.co.uk>.
Further to this, I've now changed the framework [1] so that simply newing
up the object will work (rather than requiring newTransientInstance())

There are some (relatively minor) implications to this, in the ticket.

I've also added Javadoc so that this is all more easily discoverable.

Any questions/issues, let me know

Cheers
Dan


[1] https://issues.apache.org/jira/browse/ISIS-528


On 10 September 2013 20:06, Jeremy Gurr <je...@infusionsoft.com> wrote:

> Yeah, using newTransientInstance to obtain the entity vs. newing one up
> manually worked. Thanks.
>
> Automatic recovery would be nice to make it easier for newbies to get up
> to speed with the framework. At a minimum, the javadoc for the persist
> method should indicate that you can't just persist any data entity, but
> that the entity must be obtained from the newTransientInstance method.
>
>
> ________________________________________
> From: Dan Haywood [dan@haywood-associates.co.uk]
> Sent: Tuesday, September 10, 2013 11:51 AM
> To: users
> Subject: Re: NullPointerException when trying to persist a domain entity
>
> How did you instantiate the transient object?  Did you just new it up, or
> did you use container.newTransientInstance()?
>
> I'm *hoping* the former.  The right thing to do is the latter.
>
> If the above doesn't sort it out, then could you restate the problem but
> using some different class names.  I got a bit confused with your
> description, which class is the persistable entity (cf Customer), which
> class is the domain service (cf CustomerRepo).
>
> I agree that we should improve the error messages here.  We might even try
> to automatically recover from the situation (by creating the adapter lazily
> if not present) if we are passed a pojo that was created "outside" of the
> container.  Off the top of my head, I can't see why that won't work (but I
> might be forgetting something).
>
> Cheers
> Dan
>

RE: NullPointerException when trying to persist a domain entity

Posted by Jeremy Gurr <je...@infusionsoft.com>.
Yeah, using newTransientInstance to obtain the entity vs. newing one up manually worked. Thanks. 

Automatic recovery would be nice to make it easier for newbies to get up to speed with the framework. At a minimum, the javadoc for the persist method should indicate that you can't just persist any data entity, but that the entity must be obtained from the newTransientInstance method. 


________________________________________
From: Dan Haywood [dan@haywood-associates.co.uk]
Sent: Tuesday, September 10, 2013 11:51 AM
To: users
Subject: Re: NullPointerException when trying to persist a domain entity

How did you instantiate the transient object?  Did you just new it up, or
did you use container.newTransientInstance()?

I'm *hoping* the former.  The right thing to do is the latter.

If the above doesn't sort it out, then could you restate the problem but
using some different class names.  I got a bit confused with your
description, which class is the persistable entity (cf Customer), which
class is the domain service (cf CustomerRepo).

I agree that we should improve the error messages here.  We might even try
to automatically recover from the situation (by creating the adapter lazily
if not present) if we are passed a pojo that was created "outside" of the
container.  Off the top of my head, I can't see why that won't work (but I
might be forgetting something).

Cheers
Dan

Re: NullPointerException when trying to persist a domain entity

Posted by Dan Haywood <da...@haywood-associates.co.uk>.
How did you instantiate the transient object?  Did you just new it up, or
did you use container.newTransientInstance()?

I'm *hoping* the former.  The right thing to do is the latter.

If the above doesn't sort it out, then could you restate the problem but
using some different class names.  I got a bit confused with your
description, which class is the persistable entity (cf Customer), which
class is the domain service (cf CustomerRepo).

I agree that we should improve the error messages here.  We might even try
to automatically recover from the situation (by creating the adapter lazily
if not present) if we are passed a pojo that was created "outside" of the
container.  Off the top of my head, I can't see why that won't work (but I
might be forgetting something).

Cheers
Dan


On 10 September 2013 19:08, Jeremy Gurr <je...@infusionsoft.com> wrote:

> I'm getting this when trying to run a test I'm making:
>
> java.lang.NullPointerException
> at
> org.apache.isis.core.metamodel.services.container.DomainObjectContainerDefault.persist(DomainObjectContainerDefault.java:243)
> at
> org.apache.isis.applib.AbstractContainedObject.persist(AbstractContainedObject.java:278)
>
> These line numbers are from a fresh pull performed this morning, so it's
> likely they will still be correct if you are looking at the head of the
> master branch.
>
> I'm sure I've misconfigured something, but I'm trying to understand what's
> happening here so I can fix it. It would also be nice if nulls were checked
> for in this part of code and a more useful error message thrown. Here is
> the code throwing the exception:
>
>     @Override
>     public void persist(final Object transientObject) {
>         final ObjectAdapter adapter =
> getAdapterManager().getAdapterFor(transientObject);
>         // TODO check aggregation is supported
>         if (adapter.isParented()) {
>             return;
>         }
>
> In this case, the adapter is null, and so the isParented method is called
> on a null object, throwing the exception.
>
> The transientObject being passed in here is an instance of my model class
> named ServiceClass. The service class which contains methods to create new
> instances and perform various searches is called ServiceClassService
> (analogous to TodoItems). I stepped through this code and can see the map
> it is looking in for the getAdapterFor method contains the services I
> registered, and so it could look up ServiceClassService, but not
> ServiceClass. But I'm trying to persist a data entity (ServiceClass), so
> I'm not sure what's supposed to be happening here.
>
> I have a class which extends AbstractFactoryAndRepository which of course
> has a persist method. I call that method to persist a newly created
> transient entity, but this exception is thrown. Persist seems to be the
> method I'm looking for, but why does it look to the service list to find
> which adapter to use to persist the entity? And how do I either get the
> correct service registered, or change the ServiceClassService so that it
> recognizes the ServiceClass entity as something it can work with? Sorry
> this probably doesn't make much sense, because I don't quite understand
> what the persistence code is trying to do here. Any help would be greatly
> appreciated :)
>
> -- Jeremy
>