{"record":{"id":"a0a4648c7c14e52d","repo":"OpenFeign/feign","slug":"exception-attempting-to-instantiate-s","errorCode":null,"errorMessage":"exception attempting to instantiate %s","messagePattern":"exception attempting to instantiate (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sax/src/main/java/feign/sax/SAXDecoder.java","lineNumber":175,"sourceCode":"\n      private final Constructor<ContentHandlerWithResult<T>> ctor;\n\n      private NewInstanceContentHandlerWithResultFactory(Class<ContentHandlerWithResult<T>> clazz) {\n        try {\n          this.ctor = clazz.getDeclaredConstructor();\n          // allow private or package protected ctors\n          ctor.setAccessible(true);\n        } catch (NoSuchMethodException e) {\n          throw new IllegalArgumentException(\"ensure \" + clazz + \" has a no-args constructor\", e);\n        }\n      }\n\n      @Override\n      public ContentHandlerWithResult<T> create() {\n        try {\n          return ctor.newInstance();\n        } catch (Exception e) {\n          throw new IllegalArgumentException(\"exception attempting to instantiate \" + ctor, e);\n        }\n      }\n    }\n  }\n\n  @Override\n  public boolean canDecode(Response response, Type type) {\n    return Util.isXmlContentType(response);\n  }\n}\n","sourceCodeStart":157,"sourceCodeEnd":186,"githubUrl":"https://github.com/OpenFeign/feign/blob/e2a1e27560a1e68840c34f031afca88b36096e30/sax/src/main/java/feign/sax/SAXDecoder.java#L157-L186","documentation":"feign-sax's SAXDecoder wraps a ContentHandler class that must expose a public no-arg constructor, invoked reflectively via ctor.newInstance() when a new handler instance is created for decoding. Any exception during reflective instantiation (no no-arg constructor, private constructor, constructor throwing, or class initializer failure) is rethrown as IllegalArgumentException with the constructor and cause attached. It is a configuration/class-shape error, not a runtime data error.","triggerScenarios":"Passing a class to SAXDecoder.builder() (via ContentHandlerWithResult registration / registeringContentHandler) whose declared constructor is missing, non-public, throws in its constructor or static initializer, or is an inner (non-static) class whose newInstance() call fails with InstantiationException, IllegalAccessException, or InvocationTargetException.","commonSituations":"Registering a non-static inner class as the content handler; giving the handler only a parameterized constructor; a constructor that throws NPE because a field dependency was not initialized; handler class not public.","solutions":["Give the ContentHandler class a public no-arg constructor and make the class public and static (not an inner class).","Inspect the cause (e.getCause()) in the IllegalArgumentException to see why the constructor threw and fix the constructor body.","If the handler needs dependencies, initialize them inside the handler after construction or use a builder path that supports supplied instances instead of relying on reflection."],"exampleFix":"// before\nclass OrderHandler implements ContentHandlerWithResult<Order> {\n  OrderHandler(Parser p) { ... } // no no-arg ctor\n}\n// after\npublic class OrderHandler implements ContentHandlerWithResult<Order> {\n  public OrderHandler() { ... }\n}","handlingStrategy":"validation","validationCode":"Class<?> handlerClass = OrderHandler.class;\nif (handlerClass.isMemberClass() && !java.lang.reflect.Modifier.isStatic(handlerClass.getModifiers())) {\n  throw new IllegalStateException(\"ContentHandler must be a static/top-level class\");\n}\ntry {\n  handlerClass.getDeclaredConstructor();\n} catch (NoSuchMethodException e) {\n  throw new IllegalStateException(\"ContentHandler needs a public no-arg constructor\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  Order o = client.getOrder();\n} catch (IllegalArgumentException e) {\n  if (e.getMessage() != null && e.getMessage().startsWith(\"exception attempting to instantiate\")) {\n    // fix handler class shape; log e.getCause()\n  }\n}","preventionTips":["Always make ContentHandler implementations public, static, and no-arg constructible.","Keep constructor bodies free of throwing logic; move initialization to handler methods.","Unit-test each handler registration at startup with a reflective newInstance() smoke check."],"tags":["reflection","instantiation","java","decoder","sax"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"e2a1e27560a1e68840c34f031afca88b36096e30","analyzedAt":"2026-09-10T12:37:37.238Z","contentChangedAt":"2026-09-10T12:37:37.238Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}