You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by John Kristian <jk...@linkedin.com> on 2010/08/14 02:39:37 UTC
Explaining NullPointerException
When one writes an erroneous null datum, it would be helpful for the stack
trace to say more about what happened; for example:
java.lang.NullPointerException: MyRecord.myField
at ...
Caused by: java.lang.NullPointerException: string
at ...
Is someone working to implement something like this?
One can make a subclass of SpecificDatumWriter to do this, but one must
copy-n-paste GenericDatumWriter.writeRecord. It could be done 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);
}
- John Kristian
Re: Explaining NullPointerException
Posted by Jeff Hammerbacher <ha...@cloudera.com>.
Hey John
Please file a JIRA at https://issues.apache.org/jira/browse/AVRO requesting
that this error message be clarified, and we'll get to it right away (see
https://issues.apache.org/jira/browse/AVRO-583, for example, for similar
such clarifications requested and implemented).
Thanks,
Jeff
On Fri, Aug 13, 2010 at 5:39 PM, John Kristian <jk...@linkedin.com>wrote:
> When one writes an erroneous null datum, it would be helpful for the stack
> trace to say more about what happened; for example:
>
> java.lang.NullPointerException: MyRecord.myField
> at ...
> Caused by: java.lang.NullPointerException: string
> at ...
>
> Is someone working to implement something like this?
>
> One can make a subclass of SpecificDatumWriter to do this, but one must
> copy-n-paste GenericDatumWriter.writeRecord. It could be done 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);
> }
>
> - John Kristian
>
>