Search code examples
javajooq

Null value in select produces NullPointerException in Records.mapping()


I need some null values in the select clause of my query:

dsl
        .select(
            DSL.inline(null, SQLDataType.BIGINT),
            DSL.inline(null, SQLDataType.VARCHAR),
            DSL.inline(null, SQLDataType.VARCHAR),
            DSL.inline(null, SQLDataType.INTEGER),
            CATEGORY.ABBREVIATION,
            DSL.inline(null, SQLDataType.VARCHAR),
            multiset(
                select(
                    EVENT.ABBREVIATION,
                    EVENT.NAME,
                    EVENT.GENDER,
                    EVENT.EVENT_TYPE,
                    CATEGORY_EVENT.POSITION
                )
                    .from(EVENT)
                    .join(CATEGORY_EVENT).on(CATEGORY_EVENT.EVENT_ID.eq(EVENT.ID))
                    .where(CATEGORY_EVENT.CATEGORY_ID.eq(CATEGORY.ID))
                    .orderBy(CATEGORY_EVENT.POSITION)
            ).convertFrom(r -> r.map(mapping(NumbersAndSheetsAthlete.Event::new)))
        )
        .from(CATEGORY)
        .where(CATEGORY.ID.eq(categoryId))
        .fetchOne(mapping(NumbersAndSheetsAthlete::new));

But this produces a NullPointerException in the Records.mapping method:

java.lang.NullPointerException: null
    at org.jooq.Records.lambda$mapping$16(Records.java:636) ~[jooq-3.15.0.jar:na]
    at org.jooq.impl.ResultQueryTrait.fetchOne(ResultQueryTrait.java:512) ~[jooq-3.15.0.jar:na]
    at ch.jtaf.service.NumberAndSheetsService.createDummyAthlete(NumberAndSheetsService.java:77) ~[classes/:na]

How can I use null values with Records.mapping?


Solution

  • Your NumberAndSheetsService class probably uses primitive types, and you can't map null to a primitive int.

    The confusion here might be that with jOOQ's reflection mapping, jOOQ turns null values to the primitive type's default value (e.g. 0), which has been criticised a few times as it does not follow a "fail fast" strategy: https://github.com/jOOQ/jOOQ/issues/3377

    In this case, no reflection mapping is involved, just your constructor reference, which jOOQ is not really aware of, so it can't map null to your primitive types for you.