Skip to content
This repository was archived by the owner on Mar 23, 2026. It is now read-only.
This repository was archived by the owner on Mar 23, 2026. It is now read-only.

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

@Sam-Bate-ITV

Description

@Sam-Bate-ITV

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api: bigqueryIssues related to the googleapis/java-bigquery API.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions