Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null field will lead to "java.lang.IllegalArgumentException: null" #31

Closed
radiumweilei opened this issue Oct 19, 2016 · 2 comments
Closed

Comments

@radiumweilei
Copy link

Hi,

Dalesbred is an elegant light-weigth JDBC framework, I use it on my project, and database is mysql, when I query data from table via findAll() method I encounter "java.lang.IllegalArgumentException: null" if there is null field on the return row, hope that we can fix this issue as soon as possible. Thx.

java.lang.IllegalArgumentException: null
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_25]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_25]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_25]
at java.lang.reflect.Constructor.newInstance(Constructor.java:408) ~[na:1.8.0_25]
at org.dalesbred.internal.instantiation.ReflectionInstantiator.instantiate(ReflectionInstantiator.java:62) ~[dalesbred-1.2.2.jar:na]
at org.dalesbred.internal.result.InstantiatorRowMapper.mapRow(InstantiatorRowMapper.java:76) ~[dalesbred-1.2.2.jar:na]
at org.dalesbred.result.RowMapper.lambda$list$22(RowMapper.java:59) ~[dalesbred-1.2.2.jar:na]
at org.dalesbred.result.RowMapper$$Lambda$40/852687460.process(Unknown Source) ~[na:na]
at org.dalesbred.Database.lambda$executeQuery$1(Database.java:287) [dalesbred-1.2.2.jar:na]
at org.dalesbred.Database$$Lambda$41/951880373.execute(Unknown Source) ~[na:na]
at org.dalesbred.transaction.DefaultTransaction.execute(DefaultTransaction.java:53) ~[dalesbred-1.2.2.jar:na]
at org.dalesbred.transaction.DefaultTransactionManager.withNewTransaction(DefaultTransactionManager.java:61) [dalesbred-1.2.2.jar:na]
at org.dalesbred.transaction.AbstractTransactionManager.withTransaction(AbstractTransactionManager.java:62) [dalesbred-1.2.2.jar:na]
at org.dalesbred.Database.withTransaction(Database.java:201) [dalesbred-1.2.2.jar:na]
at org.dalesbred.Database.withTransaction(Database.java:190) [dalesbred-1.2.2.jar:na]
at org.dalesbred.Database.withTransaction(Database.java:162) [dalesbred-1.2.2.jar:na]
at org.dalesbred.Database.withCurrentTransaction(Database.java:264) [dalesbred-1.2.2.jar:na]
at org.dalesbred.Database.executeQuery(Database.java:278) [dalesbred-1.2.2.jar:na]
at org.dalesbred.Database.findAll(Database.java:325) [dalesbred-1.2.2.jar:na]
at org.dalesbred.Database.findAll(Database.java:333) [dalesbred-1.2.2.jar:na]
at test.TestDalesbred.main(TestDalesbred.java:51) [main/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_25]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_25]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_25]
at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_25]
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) [idea_rt.jar:na]

@radiumweilei radiumweilei changed the title null field will lead to "java.lang.IllegalArgumentException: null" Null field will lead to "java.lang.IllegalArgumentException: null" Oct 19, 2016
@komu
Copy link
Member

komu commented Oct 19, 2016

Based on the stacktrace, I'm pretty sure that you have a constructor that requires a primitive value, but null is returned from database and therefore the constructor throws (or rather the reflection machinery trying to invoke the constructor throws).

So the same thing without Dalesbred:

public class Foo {
    public Foo(int x) { }

    public static void main(String[] args) throws Exception {
        Foo.class.getConstructors()[0].newInstance(new Object[] { null });
    }
}

Of course you'll achieve a similar result even without reflection by saying: new Foo((Integer) null).

So if constructor requires non-null value, but Dalesbred only has a null, what should it do? Isn't throwing an exception the only reasonable thing to do?

So either create a constructor that allows null values (e.g. requiring Integer instead of int or something similar) or you coalesce or similar in the database so you don't get a null back int he first place.

If I'm completely wrong about this, then I'm going to need some more details (example source code) to reproduce the bug.

@radiumweilei
Copy link
Author

Thanks Komu, I can fix this issue after follow your advice by use primitive wrapper class to instead of primitive data type, that a good news, Thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants