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

Fix npe if money is null #467

Closed
wants to merge 1 commit into from
Closed

Conversation

ugrave
Copy link
Contributor

@ugrave ugrave commented Aug 3, 2022

Closed gh-465

@vladmihalcea
Copy link
Owner

Thanks, I'll review it when I have some time.

Copy link
Contributor

@piotrooo piotrooo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ugrave great catch!

if(amount == null) {
return null;
}

String currency = rs.getString(currencyColumnName);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Null-check should be here. Because creating monetary type without currency make no sense and could rise an exception Currency Code may not be null.

I propose to use here:

if (rs.wasNull()) {
    return null;
}

Copy link
Contributor Author

@ugrave ugrave Aug 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question is should the system thow an exception if only one of them is null or still ignored it an return also null in that case. I`m was not sure about this case and decide to let the exception be thrown.

Is there a diffferend if i use rs.wasNull() or check the value of the last read column on none primitive types?
To get the correct result of rs.wasNull() i need to read the column before. It also only checks if the last read column is null.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the Java doc of the rs.getBigDicimal(x) says: if the value is SQL NULL, the value returned is null

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null seems to be enough.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we should reconsider strategy. If any of a column has null - that should be null or exception? Maybe if any value is null - null should be returned.

String amountColumnName = names[0];
String currencyColumnName = names[1];

BigDecimal amount = rs.getBigDecimal(amountColumnName);
if(amount == null) {
Copy link
Contributor

@piotrooo piotrooo Aug 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this null-check in favor of rs.wasNull() (see prev comment).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@piotrooo The wasNull method call is useful only when you have primitive values on the Java side, but this is not the case here. We can use == null checks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null seems to be enough.

@vladmihalcea
Copy link
Owner

Applied upstream, thanks.

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

Successfully merging this pull request may close these issues.

MonetaryAmountType throws NullPointerException when reading a null column value
3 participants