title: JsonMappingException: (was java.lang.ArrayIndexOutOfBoundsException)
body: <p>I have a web application where the server returns any results in JSON format. For creating the JSON, I use the <a href="http://jackson.codehaus.org/1.5.5/javadoc/org/codehaus/jackson/map/ObjectWriter.html" rel="nofollow">codehaus Jackson ObjectWriter</a>, version 1.9.8.</p>
  
  <p>The problem I'm having that sometimes there is an error, in the mapping, and from then on all server calls result in an error. I haven't been able to determine what causes the error, I did discover the origin.</p>
  
  <p>When the exception occurs, the server returns "(was java.lang.ArrayIndexOutOfBoundsException) (through reference chain: com.onior.modm.restlet.helpers.ServiceResult["success"])", which means that the exception thrown in 'toJSON' was catched and mapped to JSON by 'toRepresentation', otherwise it would have returned empty.</p>
  
  <p>I just don't know why it is sometimes failing. I will be able to use the application all morning without problems, and then suddenly I will get this error. It happens on different calls, but these calls will succeed at other times. From my point of view it seems quite random, but maybe someone can help me see the light? :)</p>
  
  <p>The server result that is being mapped:</p>
  
  <pre><code>public class ServiceResult&lt;T&gt; {
      private boolean success;
      private T results = null;
      private ModmServiceStatus message = null;
  
      public ServiceResult() {
      }
  
      public ServiceResult(T results) {
          this.success = true;
          this.results = results;
      }
  
      public ServiceResult(boolean success, ModmServiceStatus message) {
          this.success = success;
          this.message = message;
      }
  
      public ServiceResult(ServiceError error) {
          this(false, new ModmServiceStatus(error));
      }
  
      public static ServiceResult&lt;ModmServiceStatus&gt; serviceException(
          ServiceError error) {
          return new ServiceResult&lt;ModmServiceStatus&gt;(false,
              new ModmServiceStatus(error.getCode(), error.getDescription()));
      }
  
      public static ServiceResult&lt;ModmServiceStatus&gt; dbError() {
          return ServiceResult
              .serviceException(ServiceError.GENERIC_DATABASE_ERROR);
      }
  
      public static ServiceResult&lt;ModmServiceStatus&gt; invalidJson() {
          return ServiceResult
              .serviceException(ServiceError.GENERIC_INVALID_JSON);
      }
  
      public static ServiceResult&lt;ModmServiceStatus&gt; missingEntity() {
          return ServiceResult                .serviceException(ServiceError.GENERIC_MISSING_OR_INCOMPLETE_ENTITY);
      }
  
      public static ServiceResult&lt;ModmServiceStatus&gt; entityNotFound() {
          return ServiceResult
              .serviceException(ServiceError.GENERIC_ENTITY_NOT_FOUND);
      }
  
      public static ServiceResult&lt;ModmServiceStatus&gt; entityDeleted(String entity) {
          return new ServiceResult&lt;ModmServiceStatus&gt;(true,
              new ModmServiceStatus(0, entity + " deleted."));
      }
  }
  </code></pre>
  
  <p>The mapping:</p>
  
  <pre><code>public class RestUtils {
      private static final boolean PRETTY_PRINT = true;
  
      public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
      public static final ObjectWriter OBJECT_WRITER = (PRETTY_PRINT ? OBJECT_MAPPER
          .writerWithDefaultPrettyPrinter() : OBJECT_MAPPER.writer());
  
      @SuppressWarnings("unchecked")
      public static &lt;T&gt; JacksonRepresentation&lt;T&gt; toJSON(T t) throws IOException {
          JsonRepresentation jsonRepresentation = null;
          JacksonRepresentation&lt;T&gt; jacksonRepresentation = null;
          jsonRepresentation = new JsonRepresentation(
              OBJECT_WRITER.writeValueAsString(t)); // Origin of incidental
                                                      // server error
          jacksonRepresentation = new JacksonRepresentation&lt;T&gt;(
              jsonRepresentation, (Class&lt;T&gt;) t.getClass());
          return jacksonRepresentation;
      }
  
      public static &lt;T&gt; Representation toRepresentation(ServiceResult&lt;T&gt; ss) {
          Representation representation = null;
          try {
          representation = RestUtils.toJSON(ss);
          } catch (IOException jsonException) {
              jsonException.printStackTrace();
              try {
                  jsonException.printStackTrace();
                  representation = RestUtils.toJSON(jsonException.getMessage());
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
          return representation;
      }
  }
  </code></pre>
  
  <p>The call:</p>
  
  <pre><code>RestUtils.toRepresentation(new ServiceResult&lt;List&lt;Group&gt;&gt;(groups));
  </code></pre>
  
  <p>The exception with stacktrace:</p>
  
  <pre><code>org.codehaus.jackson.map.JsonMappingException: (was java.lang.ArrayIndexOutOfBoundsException) (through reference chain: com.onior.modm.restlet.helpers.ServiceResult["success"])
      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 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.ObjectWriter._configAndWriteValue(ObjectWriter.java:456)
      at org.codehaus.jackson.map.ObjectWriter.writeValueAsString(ObjectWriter.java:393)
      at com.onior.modm.restlet.helpers.RestUtils.toJSON(RestUtils.java:52)
      at com.onior.modm.restlet.helpers.RestUtils.toRepresentation(RestUtils.java:71)
      at com.onior.modm.restlet.resources.GroupCollectionResource.toJsonRead(GroupCollectionResource.java:191)
      at sun.reflect.GeneratedMethodAccessor143.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at org.restlet.resource.ServerResource.doHandle(ServerResource.java:506)
      at org.restlet.resource.ServerResource.get(ServerResource.java:707)
      at org.restlet.resource.ServerResource.doHandle(ServerResource.java:589)
      at org.restlet.resource.ServerResource.doNegotiatedHandle(ServerResource.java:649)
      at org.restlet.resource.ServerResource.doConditionalHandle(ServerResource.java:348)
      at org.restlet.resource.ServerResource.handle(ServerResource.java:952)
      at org.restlet.resource.Finder.handle(Finder.java:246)
      at org.restlet.routing.Filter.doHandle(Filter.java:159)
      at org.restlet.routing.Filter.handle(Filter.java:206)
      at org.restlet.routing.Router.doHandle(Router.java:431)
      at org.restlet.routing.Router.handle(Router.java:648)
      at org.restlet.routing.Filter.doHandle(Filter.java:159)
      at org.restlet.routing.Filter.handle(Filter.java:206)
      at org.restlet.routing.Filter.doHandle(Filter.java:159)
      at org.restlet.routing.Filter.handle(Filter.java:206)
      at org.restlet.routing.Router.doHandle(Router.java:431)
      at org.restlet.routing.Router.handle(Router.java:648)
      at org.restlet.routing.Filter.doHandle(Filter.java:159)
      at org.restlet.routing.Filter.handle(Filter.java:206)
      at org.restlet.routing.Filter.doHandle(Filter.java:159)
      at org.restlet.routing.Filter.handle(Filter.java:206)
      at org.restlet.routing.Filter.doHandle(Filter.java:159)
      at org.restlet.engine.application.StatusFilter.doHandle(StatusFilter.java:155)
      at org.restlet.routing.Filter.handle(Filter.java:206)
      at org.restlet.routing.Filter.doHandle(Filter.java:159)
      at org.restlet.routing.Filter.handle(Filter.java:206)
      at org.restlet.engine.CompositeHelper.handle(CompositeHelper.java:211)
      at org.restlet.engine.application.ApplicationHelper.handle(ApplicationHelper.java:84)
      at org.restlet.Application.handle(Application.java:381)
      at org.restlet.ext.servlet.ServletAdapter.service(ServletAdapter.java:206)
      at org.restlet.ext.spring.RestletFrameworkServlet.doService(RestletFrameworkServlet.java:124)
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
      at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
      at java.lang.Thread.run(Unknown Source)
  Caused by: java.lang.ArrayIndexOutOfBoundsException
      at org.codehaus.jackson.impl.WriterBasedGenerator.writeRaw(WriterBasedGenerator.java:577)
      at org.codehaus.jackson.util.DefaultPrettyPrinter$Lf2SpacesIndenter.writeIndentation(DefaultPrettyPrinter.java:279)
      at org.codehaus.jackson.util.DefaultPrettyPrinter.beforeObjectEntries(DefaultPrettyPrinter.java:98)
      at org.codehaus.jackson.impl.WriterBasedGenerator._writePPFieldName(WriterBasedGenerator.java:410)
      at org.codehaus.jackson.impl.WriterBasedGenerator._writeFieldName(WriterBasedGenerator.java:340)
      at org.codehaus.jackson.impl.WriterBasedGenerator.writeFieldName(WriterBasedGenerator.java:217)
      at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:444)
      at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
      ... 58 more
  </code></pre>