Skip to content

DI with typed collections - NullPointerException on TypeConverterDelegate.convertToTypedCollection [SPR-3668] #8349

Closed

Description

@spring-projects-issues

Chris Beams opened SPR-3668 and commented

I have put together and attached a simplified version of this problem with a test case that demonstrates the NullPointerException.

In the example, you'll see a generic 'Repository' interface, which extends Set<T> (and thus, "is a collection", which is apparently important in relationship to how spring wires things up in the application context)

Repository has a non-generic subinterface 'EntityRepository', declared as:

public interface EntityRepository extends Repository<Entity> { ... }

EntityRepository has two concrete implementations: JdoEntityRepository and InMemoryEntityRepository. Both extend and abstract Repository implementation (AbstractRepository<T>) as follows:

final class JdoEntityRepository extends AbstractRepository<Entity> { ... }
final class InMemoryEntityRepository extends AbstractRepository<Entity> { ... }

I have a simple beans.xml that wires up an EntityRepository, injecting it into an 'EntityUtility' bean.

When this injection is performed, I get the following stack trace:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityUtility' defined in class path resource [beans.xml]: Instantiation of bean failed; nested exception is java.lang.NullPointerExceptionCaused by: java.lang.NullPointerException at org.springframework.beans.TypeConverterDelegate.convertToTypedCollection(TypeConverterDelegate.java:395) at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:204) at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:107) at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:356) at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:438) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:153) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:799) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:717) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:384) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:156) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:287) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:91) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:75) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:65) at example.EntityRepositoryTest.demonstrateNullPointerException(EntityRepositoryTest.java:22)

IMPORTANT: If I cease to have Repository<T> extend Set<T> (and for example, have it extend Iterable<T>), the entire problem goes away. This clearly has something to do with Repository being a Collection (evidenced by the last call in the stack trace 'convertToTypedCollection()'.

Steps to reproduce the failure:

  1. Download the attached example.zip file and unpack
  2. cd spring-collections-example
  3. Using Java 5.0 and Maven 2.0.5 or better, run mvn test
  4. You'll see the 'EntityRepositoryTest.demonstrateNullPointerException()' unit test fail with the stack trace above.

Once you've seen the failure, you can make this test succeed by editing Repository.java and removing the 'extends Set<T>' from the class declaration.


Affects: 2.0.6

Attachments:

Activity

  1. spring-projects-issues commented on Jul 8, 2007

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    uploading zip containing test case mentioned in the description

  2. spring-projects-issues commented on Jul 9, 2007

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    Thanks for the report! Spring's collection conversion algorithm is indeed a bit too eager there. I've generally refined this for Spring 2.1, only trying to convert well-known collection types in the first place (skipping custom extended collection types right away).

    That said, the actual problem here is that your Set implementations returns null from its "iterator()" method - which is unexpected (invalid according to the Set contract) and makes TypeConverterDelegate choke. The value wouldn't have been replaced anyway, of course, since no actual conversion is necessary... but we don't even get to that point due to the Iterator unexpectedly being null.

    In any case, TypeConverterDelegate protects its collection conversion process against such misbehaving Collections with a null Iterator now - in 2.1 M3 as well as in 2.0.7. In addition, Spring 2.1 doesn't even try to convert unknown collections now, as indicated above.

    Juergen

  3. spring-projects-issues commented on Jul 9, 2007

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    Juergen,

    Thanks for the prompt response!

    I'm glad to report that fixing the null-returning iterator does eliminate the NPE, both in my example code as well as in my application.

    I'm especially glad to hear that custom collection types will not be specially processed going forward. I've run into other issues with this in the past and I think it's a great decision to let them be treated as 'just another bean'.

    • Chris

    Chris Beams

  4. added
    type: bug
    in: core
    on Jan 11, 2019
  5. added this to the 2.0.7 milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: coretype: bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions