avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r986959 - in /avro/trunk: CHANGES.txt lang/java/src/java/org/apache/avro/generic/GenericDatumWriter.java lang/java/src/test/java/org/apache/avro/TestSchema.java
Date Wed, 18 Aug 2010 21:44:22 GMT
Author: cutting
Date: Wed Aug 18 21:44:22 2010
New Revision: 986959

URL: http://svn.apache.org/viewvc?rev=986959&view=rev
Log:
AVRO-615. Java: Improve error message for NullPointerException while writing data.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericDatumWriter.java
    avro/trunk/lang/java/src/test/java/org/apache/avro/TestSchema.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=986959&r1=986958&r2=986959&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Wed Aug 18 21:44:22 2010
@@ -122,6 +122,9 @@ Avro 1.4.0 (unreleased)
     AVRO-616. Java: Add comment to generated source files noting that
     they should not be edited. (Patrick Wendell via cutting)
 
+    AVRO-615. Java: Improve error message for NullPointerException
+    when writing data.  (cutting)
+
   BUG FIXES
 
     AVRO-502. Memory leak from parsing JSON schema.

Modified: avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericDatumWriter.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericDatumWriter.java?rev=986959&r1=986958&r2=986959&view=diff
==============================================================================
--- avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericDatumWriter.java (original)
+++ avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericDatumWriter.java Wed Aug
18 21:44:22 2010
@@ -58,35 +58,50 @@ public class GenericDatumWriter<D> imple
   /** Called to write data.*/
   protected void write(Schema schema, Object datum, Encoder out)
     throws IOException {
-    switch (schema.getType()) {
-    case RECORD: writeRecord(schema, datum, out); break;
-    case ENUM:   writeEnum(schema, datum, out);   break;
-    case ARRAY:  writeArray(schema, datum, out);  break;
-    case MAP:    writeMap(schema, datum, out);    break;
-    case UNION:
-      int index = data.resolveUnion(schema, datum);
-      out.writeIndex(index);
-      write(schema.getTypes().get(index), datum, out);
-      break;
-    case FIXED:   writeFixed(schema, datum, out);   break;
-    case STRING:  writeString(schema, datum, out);  break;
-    case BYTES:   writeBytes(datum, out);           break;
-    case INT:     out.writeInt((Integer)datum);     break;
-    case LONG:    out.writeLong((Long)datum);       break;
-    case FLOAT:   out.writeFloat((Float)datum);     break;
-    case DOUBLE:  out.writeDouble((Double)datum);   break;
-    case BOOLEAN: out.writeBoolean((Boolean)datum); break;
-    case NULL:    out.writeNull();                  break;
-    default: error(schema,datum);
+    try {
+      switch (schema.getType()) {
+      case RECORD: writeRecord(schema, datum, out); break;
+      case ENUM:   writeEnum(schema, datum, out);   break;
+      case ARRAY:  writeArray(schema, datum, out);  break;
+      case MAP:    writeMap(schema, datum, out);    break;
+      case UNION:
+        int index = data.resolveUnion(schema, datum);
+        out.writeIndex(index);
+        write(schema.getTypes().get(index), datum, out);
+        break;
+      case FIXED:   writeFixed(schema, datum, out);   break;
+      case STRING:  writeString(schema, datum, out);  break;
+      case BYTES:   writeBytes(datum, out);           break;
+      case INT:     out.writeInt((Integer)datum);     break;
+      case LONG:    out.writeLong((Long)datum);       break;
+      case FLOAT:   out.writeFloat((Float)datum);     break;
+      case DOUBLE:  out.writeDouble((Double)datum);   break;
+      case BOOLEAN: out.writeBoolean((Boolean)datum); break;
+      case NULL:    out.writeNull();                  break;
+      default: error(schema,datum);
+      }
+    } catch (NullPointerException e) {
+      throw npe(e, " of "+schema.getName());
     }
   }
 
+  private NullPointerException npe(NullPointerException e, String s) {
+    NullPointerException result = new NullPointerException(e.getMessage()+s);
+    result.initCause(e.getCause() == null ? e : e.getCause());
+    return result;
+  }
+
   /** Called to write a record.  May be overridden for alternate record
    * representations.*/
   protected void writeRecord(Schema schema, Object datum, Encoder out)
     throws IOException {
-    for (Field field : schema.getFields()) {
-      write(field.schema(), getField(datum, field.name(), field.pos()), out);
+    for (Field f : schema.getFields()) {
+      Object value = getField(datum, f.name(), f.pos());
+      try {
+        write(f.schema(), value, out);
+      } catch (NullPointerException e) {
+        throw npe(e, " in field "+f.name());
+      }
     }
   }
   

Modified: avro/trunk/lang/java/src/test/java/org/apache/avro/TestSchema.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/test/java/org/apache/avro/TestSchema.java?rev=986959&r1=986958&r2=986959&view=diff
==============================================================================
--- avro/trunk/lang/java/src/test/java/org/apache/avro/TestSchema.java (original)
+++ avro/trunk/lang/java/src/test/java/org/apache/avro/TestSchema.java Wed Aug 18 21:44:22
2010
@@ -331,6 +331,21 @@ public class TestSchema {
     assertEquals("q.Z", ys.getField("f").schema().getFullName());
   }
 
+  @Test
+  public void testNullPointer() throws Exception {
+    String recordJson = "{\"type\":\"record\", \"name\":\"Test\", \"fields\":"
+      +"[{\"name\":\"x\", \"type\":\"string\"}]}";
+    Schema schema = Schema.parse(recordJson);
+    GenericData.Record record = new GenericData.Record(schema);
+    try {
+      checkBinary(schema, record,
+                  new GenericDatumWriter<Object>(),
+                  new GenericDatumReader<Object>());
+    } catch (NullPointerException e) {
+      assertEquals("null of string in field x of Test", e.getMessage());
+    }
+  }
+
   private static void checkParseError(String json) {
     try {
       Schema.parse(json);



Mime
View raw message