[
https://issues.apache.org/jira/browse/AVRO-615?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12898989#action_12898989
]
John Kristian commented on AVRO-615:
------------------------------------
As an experiment, I made a subclass of SpecificDatumWriter that throws
exceptions like this:
java.lang.NullPointerException: MyRecord.myField
at ...
Caused by: java.lang.NullPointerException: string
at ...
It worked, but I had to copy-n-paste GenericDatumWriter.writeRecord. I could
have done it without copy-n-pasting, if GenericDatumWriter.writeRecord were
split into two methods:
protected void writeRecord(Schema schema, Object datum, Encoder out)
throws IOException {
for (Field field : schema.getFields()) {
writeField(schema, datum, field, out);
}
}
protected void writeField(Schema schema, Object datum, Field field, Encoder
out)
throws IOException {
write(field.schema(), getField(datum, field.name(), field.pos()), out);
}
Then the subclass could override two methods:
@Override
protected void writeField(Schema schema, Object datum, Field field, Encoder
out)
throws IOException {
try {
super.writeField(schema, datum, field, out);
} catch (NullPointerException e) {
NullPointerException e2 =
new NullPointerException(schema.getName() + "." + field.name());
e2.initCause(e);
throw e2;
}
}
@Override
protected void write(Schema schema, Object datum, Encoder out)
throws IOException {
switch (schema.getType()) {
case UNION:
case NULL:
break;
default:
if (datum == null)
throw new NullPointerException(schema.getName());
}
super.write(schema, datum, out);
}
> Explain NullPointerException
> ----------------------------
>
> Key: AVRO-615
> URL: https://issues.apache.org/jira/browse/AVRO-615
> Project: Avro
> Issue Type: Improvement
> Components: java
> Affects Versions: 1.3.3
> Reporter: John Kristian
>
> Please provide more information in the exception that results from attempting
> to write a null value that the schema doesn't allow. For example, include
> the names of the type, the field and the enclosing record type.
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.