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

FieldValueList & FieldValue throwing null pointer exception when getting value of nullable field #3179

Closed
Sam-Bate-ITV opened this issue Mar 4, 2024 · 3 comments · Fixed by #3255
Labels
api: bigquery Issues related to the googleapis/java-bigquery API.

Comments

@Sam-Bate-ITV
Copy link

I have a table with numerous nullable fields but when using the Java api to get the data from a row I've found that instead of simply getting null instead of a string the code throws an exception.

/**
* Returns this field's value as a {@link String}. This method should only be used if the
* corresponding field has primitive type ({@link LegacySQLTypeName#BYTES}, {@link
* LegacySQLTypeName#BOOLEAN}, {@link LegacySQLTypeName#STRING}, {@link LegacySQLTypeName#FLOAT},
* {@link LegacySQLTypeName#INTEGER}, {@link LegacySQLTypeName#NUMERIC} {@link
* LegacySQLTypeName#TIMESTAMP}).
*
* @throws ClassCastException if the field is not a primitive type
* @throws NullPointerException if {@link #isNull()} returns {@code true}
*/
@SuppressWarnings("unchecked")
public String getStringValue() {
checkNotNull(value);
return (String) value;
}

As users are likely to want to map all results from a query regardless of whether the field was null or not it would be better if the methods for getting a String or Long handled null gracefully. Or at least provide an alternative method where the caller can provide a default value or null if the fields value is null.

Fortunately as I'm using Kotlin I can work around this for now with an extension function on FieldValueList:

fun FieldValueList.getNullable(name: String): FieldValue? = if (this.get(name).isNull) null else this.get(name)

which in turn means I can simply do row.getNullable("my_nullable_column")?.stringValue when mapping the FieldValueList to an object.

If getStringValue() had on overloaded method that takes an arg that's used as a default I could have done something like:

row.get("my_nullable_column").getStringValue(null)

which would be convenient. Also this could be added without breaking the existing API.

@product-auto-label product-auto-label bot added the api: bigquery Issues related to the googleapis/java-bigquery API. label Mar 4, 2024
@PhongChuong
Copy link
Contributor

Thank you for the feedback.
I'll discuss this with the team and get back to you soon.

@PhongChuong
Copy link
Contributor

Hi @Sam-Bate-ITV ,
Would it preferable for your use case to:

  1. Provide an overloaded getStringValue (from the post) or new method getStringValueOrDefault with similar behavior
  2. Provide a knob to turn off the checkNotNull check

@Sam-Bate-ITV
Copy link
Author

either would be fine for me as long as it's consistent with other methods that allow a default fallback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: bigquery Issues related to the googleapis/java-bigquery API.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants