[
https://issues.apache.org/jira/browse/AVRO-1948?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15635941#comment-15635941
]
Tom White commented on AVRO-1948:
---------------------------------
{{GenericRecordBuilder}} will fill in in default fields for you, while
{{GenericData.Record}} does not. The javadoc for the {{GenericData.Record}}
mentions this, but it's easy to miss it.
> Not setting a non-null field in GenericRecord causes a NullPointer exception
> on Serialisation even though a default is defined in the schema
> --------------------------------------------------------------------------------------------------------------------------------------------
>
> Key: AVRO-1948
> URL: https://issues.apache.org/jira/browse/AVRO-1948
> Project: Avro
> Issue Type: Bug
> Components: java
> Affects Versions: 1.8.1
> Environment: Windows 7, JDK8
> Reporter: Mike Hurd
> Priority: Minor
>
> As per the summary, I had a search through the existing JIRAs but couldn't
> find a match apologies if this is a duplicate:
> example schema:
> {noformat}
> {"namespace": "example.data",
> "type": "record",
> "name": "Data",
> "fields": [
> {"name": "name", "type": "string"},
> {"name": "favorite_season", "type": "string", "default": "Autumn"}
> ]
> }
> {noformat}
> {noformat}
> GenericRecord record = new GenericData.Record(schema);
> record.put("name", "my name");
> {noformat}
> Serialising the above throws a NullPointerException:
> {noformat}
> java.lang.NullPointerException: null of string in field favorite_season of
> example.data.Data
> at
> org.apache.avro.generic.GenericDatumWriter.npe(GenericDatumWriter.java:145)
> ~[classes/:?]
> at
> org.apache.avro.generic.GenericDatumWriter.writeWithoutConversion(GenericDatumWriter.java:139)
> ~[classes/:?]
> at
> org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:75)
> ~[classes/:?]
> at
> org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:62)
> ~[classes/:?]
> {noformat}
> Surely, given that the schema is available the writeField method should check
> to see if there is a default field available to use if the required field
> does not have a value supplied (and is not-null).
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)