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

MonetaryAmountType throws NullPointerException when reading a null column value #465

Closed
ugrave opened this issue Aug 2, 2022 · 5 comments
Labels
Milestone

Comments

@ugrave
Copy link
Contributor

ugrave commented Aug 2, 2022

The MonetaryAmountType use the ResulSet.wasNull to check for null.
Problem is that at least one getter must called before.

From javadoc og ResultSet.wasNull:
Note that you must first call one of the getter methods on a column to try to read its value

I attached a demo with two use cases.
One read only the Money value there other one reads the hole entity.
When reading only the money value the npe is thrown. Only whan an additional column was read before it works without exception.

demo.zip

@vladmihalcea
Copy link
Owner

I don't understand the issue. There is a integration test in repository you could use to add a new test that replicates your issue.

Not only that it's easier to isolate the problem that way, but, after the fix is done, there is already a test to prove the fix.

@ugrave
Copy link
Contributor Author

ugrave commented Aug 2, 2022

Hi,

added a test case which reproduce the error.

https://github.com/ugrave/hibernate-types/blob/gh-465/hibernate-types-55/src/test/java/com/vladmihalcea/hibernate/type/money/PostgreSQLMonetaryAmountTypeTest.java#L69

Found out that this always happend if the salery is null.
Another problem i found out during testing is that if you have an additional field in the entity which is null, the money type does not read the money data from the result set. Also if the money columns are not null.

Have added this also to the test case.

@ugrave
Copy link
Contributor Author

ugrave commented Aug 2, 2022

A fix could be to check the amaout value if it is not null instead of the rs.wasNull().
Or first read the amount column and the check with rs.wasNull().

@vladmihalcea
Copy link
Owner

Thanks @ugrave for the tests and the investigation. I'll check it out when I have some time.

In the meanwhile, if you want, you could try to provide a Pull Request with a fix proposal.

ugrave added a commit to ugrave/hibernate-types that referenced this issue Aug 3, 2022
@vladmihalcea vladmihalcea changed the title MonetaryAmountType throws null pointer exception if read for db MonetaryAmountType throws NullPointerException when reading a null column value Aug 3, 2022
@vladmihalcea vladmihalcea added this to the 2.17.2 milestone Aug 3, 2022
@vladmihalcea
Copy link
Owner

Fixed.

Repository owner deleted a comment from RoarBjur Aug 3, 2022
Repository owner deleted a comment from RoarBjur Aug 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants