theonedev/onedev · error · IllegalStateException

Attempt to set a model object on a component without a model

Error message

Attempt to set a model object on a component without a model! 

What it means

Wicket's setModelObject() (and model-changing setters) require the component to have an IModel assigned. When getDefaultModel() returns null there is nothing to set the object into, so Wicket throws IllegalStateException and points you at the component's page-relative path. The library does this because a plain component constructed without a model has no backing storage for the object.

Source

Thrown at server-core/src/main/java/org/apache/wicket/Component.java:3130

	 * Sets the backing model object. Unlike <code>getDefaultModel().setObject(object)</code>, this
	 * method checks authorisation and model comparator, and invokes <code>modelChanging</code> and
	 * <code>modelChanged</code> if the value really changes.
	 * 
	 * @param object
	 *            The object to set
	 * @return This
	 * @throws IllegalStateException If the component has neither its own model nor any of its
	 * parents uses {@link IComponentInheritedModel}
	 */
	@SuppressWarnings("unchecked")
	public final Component setDefaultModelObject(final Object object)
	{
		final IModel<Object> model = (IModel<Object>)getDefaultModel();

		// Check whether anything can be set at all
		if (model == null)
		{
			throw new IllegalStateException(
				"Attempt to set a model object on a component without a model! " +
				"Either pass an IModel to the constructor or use #setDefaultModel(new SomeModel(object)). " +
				"Component: " + getPageRelativePath());
		}

		// Check authorization
		if (!isActionAuthorized(ENABLE))
		{
			throw new UnauthorizedActionException(this, ENABLE);
		}

		// Check whether this will result in an actual change
		if (!getModelComparator().compare(this, object))
		{
			modelChanging();
			model.setObject(object);
			modelChanged();
		}

View on GitHub (pinned to d44925c47c)

Solutions

  1. Pass an IModel to the component constructor: new Label("id", model) or new Label("id", someObject).
  2. Call component.setDefaultModel(new Model/myModel(object)) before setModelObject is invoked.
  3. If the component should be static, remove the setModelObject call instead of adding a model.

Example fix

// before
Label label = new Label("name");
label.setModelObject("Alice");
// after
Label label = new Label("name", Model.of("Alice"));
label.setModelObject("Alice"); // now legal
Defensive patterns

Strategy: validation

Validate before calling

if (component.getDefaultModel() == null) {
    component.setDefaultModel(Model.of("")); // or pass model in constructor
}
component.setModelObject(value);

Type guard

if (!(component instanceof Page) && component.getDefaultModel() == null) { /* supply model first */ }

Try / catch

try { component.setModelObject(value); } catch (IllegalStateException e) { component.setDefaultModel(Model.of(value)); }

Prevention

When it happens

Trigger: Calling component.setModelObject(obj) or setObject() on a model when the component was constructed with only a String id, e.g. new Label("myLabel") with no IModel, then attempting to set its value.

Common situations: Constructing components with just an id during quick prototyping; moving code around so the model assignment was dropped; calling setModelObject from an event handler (link onClick, form onSubmit) on a component created elsewhere without a model.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of theonedev/onedev@d44925c47c (2026-09-06). Data as JSON: /api/errors/9a6922eeef92e3e1. Report an issue: GitHub.