title: Jackson & JSONAnySetter: NullPointer Exception during Serialization/Deserialization
body: <p>I have a problem with serialization/deserialization with Jackson 1.9.13 (and Jackson 2.5.0) and fighting this now for a few days without any success.</p>
  
  <p>My goal is to use @JsonAnyGetter &amp; @JsonAnySetter, and I want to calculate dynamically if a object should be written to the output or not. I have a JSON definition which I serialize with the ObjectMapper (and check if the Object should be included or not), and then I convert the object back to a string.</p>
  
  <p>I am using a "HidableSerializer" for this, which works fine during serialization, but not when converting the object back to a string. </p>
  
  <p>Without @JsonAnySetter / -getter <strong>or</strong> the "HidableSerializer", everythings works fine, but not both together.</p>
  
  <p>Why is this not working? And how can I solve the problem? Better approaches are welcome!</p>
  
  <p>The stack trace looks like this:</p>
  
  <p><strong>Stack Trace</strong></p>
  
  <pre><code>org.codehaus.jackson.map.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: ch.hasselba.Test["[anySetter]"])
  null
      at org.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:218)
      at org.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:183)
      at org.codehaus.jackson.map.ser.std.SerializerBase.wrapAndThrow(SerializerBase.java:140)
      at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:158)
      at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
  at ch.hasselba.HidableSerializer.serialize(HidableSerializer.java:29)
      at org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:610)
      at org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:256)
      at org.codehaus.jackson.map.ObjectMapper._configAndWriteValue(ObjectMapper.java:2575)
      at org.codehaus.jackson.map.ObjectMapper.writeValueAsString(ObjectMapper.java:2097)
      at ch.hasselba.Demo.main(Demo.java:54)
  Caused by: java.lang.NullPointerException
      at org.codehaus.jackson.map.ser.std.MapSerializer.serializeFields(MapSerializer.java:243)
      at org.codehaus.jackson.map.ser.AnyGetterWriter.getAndSerialize(AnyGetterWriter.java:41)
      at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:154)
      ... 7 more
  </code></pre>
  
  <p><strong>The Demo code</strong></p>
  
  <pre><code>package ch.hasselba;
  
  import org.codehaus.jackson.Version;
  import org.codehaus.jackson.map.JsonSerializer;
  import org.codehaus.jackson.map.ObjectMapper;
  import org.codehaus.jackson.map.SerializationConfig;
  import org.codehaus.jackson.map.introspect.BasicBeanDescription;
  import org.codehaus.jackson.map.module.SimpleModule;
  import org.codehaus.jackson.map.ser.BeanSerializerModifier;
  
  public class Demo {
  
      public static void main(String[] args) {
  
          ObjectMapper mapper = new ObjectMapper();
  
          // register the module
          Version version = new Version(1, 0, 0, "SNAPSHOT");
          mapper.registerModule(new SimpleModule("HidableModule", version) {
              @Override
              public void setupModule(SetupContext context) {
                  super.setupModule(context);
                  context.addBeanSerializerModifier(new BeanSerializerModifier() {
                      @SuppressWarnings("unchecked")
                      @Override
                      public JsonSerializer&lt;?&gt; modifySerializer(SerializationConfig config, BasicBeanDescription desc,
                          JsonSerializer&lt;?&gt; serializer) {
                          if (IHidable.class.isAssignableFrom(desc.getBeanClass())) {
                              return new HidableSerializer&lt;Object&gt;((JsonSerializer&lt;Object&gt;) serializer);
                          }
                          return serializer;
                      }
                  });
              }
          });
  
          // the data
          String content = "{ \"foo\": \"bar\" }";
  
          // build the Object
          Test test = null;
          try {
              test =  mapper.readValue(content, Test.class);
          } catch (Exception e) {
              e.printStackTrace();
          }
  
          // and now convert it back to a String
          String data = null;
          try {
               data = mapper.writeValueAsString(test);
          } catch (Exception e) {
              e.printStackTrace();
          }
  
          System.out.println( data );
  
      }
  
  }
  </code></pre>
  
  <p><strong>Test class</strong></p>
  
  <pre><code>package ch.hasselba;
  
  import java.util.Map;
  import java.util.concurrent.ConcurrentHashMap;
  
  import org.codehaus.jackson.annotate.JsonAnyGetter;
  import org.codehaus.jackson.annotate.JsonAnySetter;
  
  public class Test implements IHidable {
  
      private Map&lt;String, Object&gt; others = new ConcurrentHashMap&lt;String, Object&gt;();
  
      @JsonAnyGetter
      public Map&lt;String, Object&gt; getOthers() {
          return this.others;
      }
  
      @JsonAnySetter
      public void addOther(final String name, final Object value) {   
          this.others.put(name, value);
      }
  
      @Override
      public boolean isHidden() {
          return false;
      }
  
  }
  </code></pre>
  
  <p><strong>The Hidable Serializer</strong></p>
  
  <pre><code>package ch.hasselba;
  
  import java.io.IOException;
  
  import org.codehaus.jackson.JsonGenerator;
  import org.codehaus.jackson.JsonProcessingException;
  import org.codehaus.jackson.map.JsonSerializer;
  import org.codehaus.jackson.map.SerializerProvider;
  
  public class HidableSerializer&lt;T&gt; extends JsonSerializer&lt;T&gt; {
  
      private JsonSerializer&lt;T&gt; defaultSerializer;
  
      public HidableSerializer(JsonSerializer&lt;T&gt; serializer) {
          defaultSerializer = serializer;
      }
  
      @Override
      public void serialize(T value, JsonGenerator jgen, SerializerProvider provider)
              throws IOException, JsonProcessingException {
  
          if( value instanceof IHidable ){
              IHidable hidableValue = (IHidable) value;
              if( hidableValue.isHidden() )
                  return;
          }
          defaultSerializer.serialize(value, jgen, provider);
  
      }
  
  }
  </code></pre>
  
  <p><strong>IHidableInterface</strong></p>
  
  <pre><code>package ch.hasselba;
  
  
  public interface IHidable {
      boolean isHidden();
  }
  </code></pre>