[ 
https://issues.apache.org/jira/browse/AVRO-1807?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15275869#comment-15275869
 ] 

Ryan Blue commented on AVRO-1807:
---------------------------------

[~thms], from looking at that stack trace, it looks like the object you passed 
in, payload, is generating a null json object. The trace is pretty 
straight-forward: the object you pass in is handed to JacksonUtils.toJsonNode, 
which succeeds. Then the value returned by that method is passed to Json.write 
and immediately throws the NPE because it is null when asToken is called on it 
on line 183. That means the result of toJsonNode must be null.

That would happen in two cases. First, the object you passed could be null, but 
that is unlikely because of your print statement. The second case is where Avro 
doesn't recognize the object you passed in. The code for 
[toJson|https://github.com/apache/avro/blob/master/lang/java/avro/src/main/java/org/apache/avro/util/internal/JacksonUtils.java#L57-86]
 is a series of if clauses for supported classes, like Map or Integer, with no 
final else to catch unmatched objects. I think the problem is that you're 
passing in a specific object, which isn't supported here. I think you want to 
be using the SpecificDatumWriter for specific objects with the JsonEncoder, 
rather than the ObjectWriter.

I think this does expose a minor bug in Avro, which is that this method 
silently returns null when the object isn't recognized. If this happened to a 
nested field that allowed null, it looks like it would silently ignore the 
field. I'll add an else clause that throws an exception in that case.

> NullPointerException from Json.ObjectWriter
> -------------------------------------------
>
>                 Key: AVRO-1807
>                 URL: https://issues.apache.org/jira/browse/AVRO-1807
>             Project: Avro
>          Issue Type: Bug
>          Components: java
>    Affects Versions: 1.8.1
>         Environment: avro 1.8.0  
> jackson-core-asl 1.9.13  
> jackson-mapper-asl 1.9.13  
> Java 7  
> Mac OS X 10.11.3
>            Reporter: Thomas Lörtsch
>            Priority: Blocker
>
> /* I posted this bug already to the dev-mailinglist [4]. Reporting it here 
> again only to make sure it doesn't get lost and because this is the right 
> place. */
> The complete, slightly more involved code is on [1], especially [2], the JSON 
> schema is at [3], but the relevant parts of the code follow below.
> {code}
> // parsing the schema
> Schema.Parser parser = new Schema.Parser();
> Schema schema = parser.parse(new File("schema.avsc")) ;
> // setting up the encoder and driver
> Json.ObjectWriter jsonDatumWriter = new Json.ObjectWriter();
> OutputStream output = new FileOutputStream(new File("output.json"));
> Encoder encoder = EncoderFactory.get().jsonEncoder(schema, output, pretty);
> // writing
> System.out.println(payload); // reassuring test the payload is intact
> jsonDatumWriter.write(payload, encoder);
> {code}
> The console will print out a nice JSON string (the payload), followed by the 
> following exception:
> {code}
> Exception in thread "main" java.lang.NullPointerException
> at org.apache.avro.data.Json.write(Json.java:183)
> at org.apache.avro.data.Json.writeObject(Json.java:272)
> at org.apache.avro.data.Json.access$000(Json.java:48)
> at org.apache.avro.data.Json$ObjectWriter.write(Json.java:122)
> at converTor.WriterObject.append(WriterObject.java:59)
> at converTor.ConverTor.main(ConverTor.java:251)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at 
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
> at 
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> at java.lang.reflect.Method.invoke(Method.java:606)
> at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
> {code}
> My beginners guess is that the source of the problem is a call of asToken() 
> in org.codehaus.jackson.JsonNode, which is abstract.
> [0] 
> https://issues.apache.org/jira/browse/avro/?selectedTab=com.atlassian.jira.jira-projects-plugin:summary-panel
> [1] https://github.com/tomlurge/converTor
> [2] 
> https://github.com/tomlurge/converTor/blob/master/src/converTor/WriterObject.java
> [3] 
> https://github.com/tomlurge/converTor/blob/master/src/converTor/avro/schemata/Torperf.avsc
> [4] 
> https://mail-archives.apache.org/mod_mbox/avro-dev/201603.mbox/ajax/%3C828828B1-8A58-4050-81B4-C3EF0F26041B%40rat.io%3E



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to