code4craft/webmagic · error · IllegalStateException

Can't find formatter for field of type

Error message

Can't find formatter for field  of type 

What it means

ObjectFormatterBuilder.initFormatterForType looks up a registered ObjectFormatter for the field's basic type. If the field type is not String, not a List, and has no registered formatter for its detected basic class, IllegalStateException is thrown — the model framework cannot convert extracted strings into that field type. (The message text omits field name/type interpolation in this version.)

Solutions

  1. Change the field type to String (or List<String>) and convert manually afterwards
  2. Register a custom ObjectFormatter for the type via ObjectFormatters before creating the extractor
  3. Use a supported basic type (e.g. int/long/boolean wrappers that have formatters) or add a getter-based conversion

Example fix

// before
private MyPojo detail;
// after
private String detail; // convert to MyPojo after extraction
Defensive patterns

Strategy: validation

Validate before calling

Class<?> t = field.getType(); if (!String.class.equals(t) && !List.class.isAssignableFrom(t) && ObjectFormatters.get(BasicTypeFormatter.detectBasicClass(t)) == null) { /* unsupported field type */ }

Type guard

boolean hasFormatter(Class<?> t) { return String.class.equals(t) || List.class.isAssignableFrom(t) || ObjectFormatters.get(BasicTypeFormatter.detectBasicClass(t)) != null; }

Try / catch

try { extractor = PageModelExtractor.create(Model.class); } catch (IllegalStateException e) { log.error("no formatter for a model field type", e); }

Prevention

When it happens

Trigger: Declaring a model field of an unsupported type such as Date with an unregistered format, custom POJO, Integer variant without a formatter, then running a page-model spider so build()/initFormatterForType executes.

Common situations: Model beans with fields typed Long/Double/Boolean beyond registered formatters, custom enum or POJO fields, or Date fields after the date-format handling changed between WebMagic versions.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of code4craft/webmagic@67816a19d6 (2026-09-08). Data as JSON: /api/errors/59f2420fc87062b2. Report an issue: GitHub.

Appendix: source

Thrown at webmagic-extension/src/main/java/us/codecraft/webmagic/model/formatter/ObjectFormatterBuilder.java:28

 * @since 0.7.0
 *         Date: 2017/6/3
 */
public class ObjectFormatterBuilder {

    private Field field;

    public ObjectFormatterBuilder setField(Field field) {
        this.field = field;
        return this;
    }

    private ObjectFormatter initFormatterForType(Class<?> fieldClazz, String[] params) {
        if (fieldClazz.equals(String.class) || List.class.isAssignableFrom(fieldClazz)){
            return null;
        }
        Class<? extends ObjectFormatter> formatterClass = ObjectFormatters.get(BasicTypeFormatter.detectBasicClass(fieldClazz));
        if (formatterClass == null) {
            throw new IllegalStateException("Can't find formatter for field " + field.getName() + " of type " + fieldClazz);
        }
        return initFormatter(formatterClass, params);
    }

    private ObjectFormatter initFormatter(Class<? extends ObjectFormatter> formatterClazz, String[] params) {
        try {
            ObjectFormatter objectFormatter = formatterClazz.newInstance();
            objectFormatter.initParam(params);
            return objectFormatter;
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public ObjectFormatter build() {
        Formatter formatter = field.getAnnotation(Formatter.class);

View on GitHub (pinned to 67816a19d6)