{"record":{"id":"06c30b65af07f1fe","repo":"dotnet/aspnetcore","slug":"typereference-must-be-instantiated-with-a-type-par","errorCode":null,"errorMessage":"TypeReference must be instantiated with a type parameter such as (new TypeReference<Foo<Bar>>() {}).","messagePattern":"TypeReference must be instantiated with a type parameter such as \\(new TypeReference<Foo<Bar>>\\(\\) (.+?)\\)\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/SignalR/clients/java/signalr/core/src/main/java/com/microsoft/signalr/TypeReference.java","lineNumber":40,"sourceCode":"     * spite of type erasure since, sadly, {@code Foo<Bar>.class} is not valid Java.\n     *\n     * To get the Type of Class {@code Foo<Bar>}, use the following syntax:\n     * <pre>{@code\n     * Type fooBarType = (new TypeReference<Foo<Bar>>() { }).getType();\n     * }</pre>\n     *\n     * To get the Type of class Foo, use a regular Type Token:\n     * <pre>{@code\n     * Type fooType = Foo.class;\n     * }</pre>\n     *\n     *  @see <a href=\"http://gafter.blogspot.com/2006/12/super-type-tokens.html\">Super Type Tokens</a>\n     */\n    public TypeReference() {\n        try {\n            this.type = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];\n        } catch (ClassCastException ex) {\n            throw new RuntimeException(\"TypeReference must be instantiated with a type parameter such as (new TypeReference<Foo<Bar>>() {}).\");\n        }\n    }\n\n    /**\n     * Gets the referenced type.\n     * @return The Type encapsulated by this TypeReference\n     */\n    public Type getType() {\n        return this.type;\n    }\n}\n","sourceCodeStart":22,"sourceCodeEnd":52,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/SignalR/clients/java/signalr/core/src/main/java/com/microsoft/signalr/TypeReference.java#L22-L52","documentation":"TypeReference<T> uses the super-type-token pattern: it captures T by reflecting the generic superclass of an anonymous subclass. If the subclass is not parameterized (e.g. 'new TypeReference()' with no <...>), getGenericSuperclass() is a Class, not a ParameterizedType, so the cast throws ClassCastException which is caught and rethrown as a RuntimeException with this message.","triggerScenarios":"Instantiating 'new TypeReference()' (raw) instead of 'new TypeReference<Map<String,Integer>>() {}'. Also reached if someone subclasses TypeReference without specifying the type parameter.","commonSituations":"Copy-paste error omitting the generic; using TypeReference as a field of a non-parameterized class; refactor that lost the angle brackets; tooling/codegen that strips generics.","solutions":["Always create TypeReference as an anonymous inner class WITH the type argument: new TypeReference<List<Integer>>() {}.","Store the instance in a field typed with the parameter so the compiler keeps it: TypeReference<List<Integer>> listType = new TypeReference<List<Integer>>() {};.","If you need a non-generic type, pass the Class directly (e.g. invoke(String.class, ...)) instead of TypeReference."],"exampleFix":"// before\nTypeReference ref = new TypeReference(); // raw -> throws\n\n// after\nTypeReference<List<Integer>> ref = new TypeReference<List<Integer>>() {};","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"// Validate at construction that the subclass is parameterized.\npublic static <T> TypeReference<T> checked(TypeReference<T> ref) {\n  if (ref.getType() == null) {\n    throw new IllegalArgumentException(\"TypeReference missing type parameter\");\n  }\n  return ref;\n}","tryCatchPattern":"try {\n  TypeReference<?> ref = new TypeReference() {}; // raw\n} catch (RuntimeException e) {\n  if (e.getMessage().contains(\"TypeReference must be instantiated\")) {\n    // guide developer to add the type parameter\n  } else throw e;\n}","preventionTips":["Always write 'new TypeReference<X>() {}' with the angle brackets.","Assign to a parameterized field so the IDE keeps the generic.","Prefer Class-based overloads when the type is non-generic."],"tags":["signalr","java","generics","typereference","api-misuse"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}