0

I have the following code snippet:

public List<EntityA> getAllEntityAByAAndDPaginated(
        Collection<Long> a, 
        Collection<Long> d, 
        int numberOfResults, 
        int page
    ) {
            TypedQuery<EntityA> q = entityManager.createNamedQuery(EntityA.GET_BY_A_AND_D,
                    EntityA.class);
            q.setParameter(A, a);
            q.setParameter(D, d);
            q.setMaxResults((page + 1) * numberOfResults * d.size()); // without this, it works!
            return a != null && !a.isEmpty() && d != null && !d.isEmpty()
                    ? q.getResultList() : new ArrayList<>();
    }

For performance reasons, I am only loading (page + 1) * numberOfResults * d.size() results. But executing this, I get a NullPointerException in the last line of the code snippet of the following form:

java.lang.NullPointerException
at java.base/java.util.ArrayList.<init>(ArrayList.java:179)
at org.eclipse.persistence.mappings.ForeignReferenceMapping.prepareNestedJoinQueryClone(ForeignReferenceMapping.java:2455)
at org.eclipse.persistence.mappings.OneToOneMapping.valueFromRowInternalWithJoin(OneToOneMapping.java:1814)
at org.eclipse.persistence.mappings.ForeignReferenceMapping.valueFromRow(ForeignReferenceMapping.java:2177)
at org.eclipse.persistence.mappings.ForeignReferenceMapping.buildCloneFromRow(ForeignReferenceMapping.java:341)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildAttributesIntoWorkingCopyClone(ObjectBuilder.java:2007)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildWorkingCopyCloneFromRow(ObjectBuilder.java:2260)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObjectInUnitOfWork(ObjectBuilder.java:858)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:745)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:699)
at ...

According the exception stacktrace, it seems like it has tried somewhere to add null values to the result list. When I delete the part q.setMaxResults((page + 1) * numberOfResults * d.size());, it works fine, i.e. this has to cause the NPE somehow.

The database of EntityA does not contain any null values.

How can I fix this?

Chris
  • 20,138
  • 2
  • 29
  • 43
ATW
  • 233
  • 2
  • 14
  • 2
    How come you are checking `d != null` the line after an unguarded call to `d.size()`? – khelwood Feb 14 '23 at 09:08
  • same thoughts. log all the incoming parameters to know which are nulls. or run it in a debug mode. d.size() is a main suspect – gneginskiy Feb 14 '23 at 09:10
  • @khelwood Yes, thank you, that's a code point but neither a nor d is null when I run it correctly. But you are right, I have to do the check somewhere further up but the NPE remains the same... I'm just confused how the setMaxResults(...) can cause a NPE when neither page, nor numberOfResults, nor d are null... – ATW Feb 14 '23 at 09:21
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – fantaghirocco Feb 14 '23 at 09:31
  • @fantaghirocco No. It seems like the NPE is thrown somewhere in the TypedQuery class. As I said, neither a, nor d, nor page, nor numberOfResults is null und the NPE is thrown anyway. – ATW Feb 14 '23 at 09:35
  • What version of EclipseLink are you using? The stack shows the NPE when building the results and working over a OneToOne relationship - maybe show the entities and the resulting SQL ( https://wiki.eclipse.org/EclipseLink/Examples/JPA/Logging ) The maxResult value you use seems odd - why are you putting in d.size() in there at all? Are you trying to account for a cartesian join somehow? Maybe try using just the absolute number of As you want to return and specify the offset to skip to the current page of values. – Chris Feb 14 '23 at 14:55

0 Answers0