NullPointerException when fetching into a POJO

721 views
Skip to first unread message

c.h...@greylogix.de

unread,
Mar 20, 2015, 6:08:12 AM3/20/15
to jooq...@googlegroups.com
When a record is null after DSL select query the method into() will throw a NullPointerException

into() should throw any other BusinessException or return NULL

Lukas Eder

unread,
Mar 20, 2015, 6:29:16 AM3/20/15
to jooq...@googlegroups.com
Hi,

it's always a good idea to accompany a bug report with actual code. Code is better at describing code, than words... :)

I'm assuming that you're using this code:

MyType object = DSL.using(...)
                   .select(...)
                   .from(...)
                   .fetchOne()
                   .into(MyType.class);

Yes. That will throw a NullPointerException because fetchOne() may return null. There is no way to throw any other exception on the into() call, as we cannot intercept calls made on null references in Java. Unfortunately, we're still supporting Java 6, so we do not have access to the Java 8 Optional type, which might have been used here.

I suggest you either handle null references yourself, or you resort to using fetchOneInto():

MyType object = DSL.using(...)
                   .select(...)
                   .from(...)
                   .fetchOneInto(MyType.class);

Hope this helps,
Lukas

2015-03-20 10:17 GMT+01:00 <c.h...@greylogix.de>:
When a record is null after DSL select query the method into() will throw a NullPointerException

into() should throw any other BusinessException or return NULL

--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

chapke.g...@gmail.com

unread,
May 4, 2015, 11:35:22 AM5/4/15
to jooq...@googlegroups.com
Hi Lukas,

sorry, I forgot to send a code snippet.

In my case the optional type is not a good idea cause the ID which should match to one object have to exist.
But I understand my mistake and I like the 'fetchOneInto' method.

Thank you very much!

Lukas Eder

unread,
May 4, 2015, 11:41:08 AM5/4/15
to jooq...@googlegroups.com
Hello,

2015-05-04 17:29 GMT+02:00 <chapke.g...@gmail.com>:
Hi Lukas,

sorry, I forgot to send a code snippet.

No worries ;-)
 
In my case the optional type is not a good idea cause the ID which should match to one object have to exist.
But I understand my mistake and I like the 'fetchOneInto' method.

I see. Yes, Optional would be wrong, then.

Note that we also have a fetchExactlyOne() method on the roadmap (naming may vary):

But that will also throw an exception in case no record was encountered...
Reply all
Reply to author
Forward
0 new messages