NationalSecurityAgency/ghidra · error · ServiceConstructionException

Error constructing dependent service via {}

Error message

Error constructing dependent service via {}

What it means

Thrown by DependentServiceConstructor.construct() when invoking the @DependentService factory method raises an exception (caught as InvocationTargetException). The original cause is unwrapped and wrapped in a ServiceConstructionException, preserving the offending method in the message. This is a runtime failure inside the user-supplied factory, not a wiring error.

Source

Thrown at Ghidra/Debug/ProposedUtils/src/main/java/generic/depends/DependentServiceConstructor.java:50

		}
		this.cls = cls;
		this.method = method;
	}

	@SuppressWarnings("unchecked")
	T construct(Object obj, Map<Class<?>, Object> dependencies)
			throws ServiceConstructionException {
		List<Object> params = new ArrayList<>(method.getParameterCount());
		for (Class<?> pType : method.getParameterTypes()) {
			Object p = dependencies.get(pType);
			assert p != null;
			params.add(p);
		}
		try {
			return (T) method.invoke(obj, params.toArray());
		}
		catch (InvocationTargetException e) {
			throw new ServiceConstructionException(
				"Error constructing dependent service via " + method, e.getCause());
		}
		catch (IllegalAccessException | IllegalArgumentException e) {
			throw new AssertionError(e);
		}
	}
}

View on GitHub (pinned to d5f144c24d)

Solutions

  1. Inspect the wrapped cause (ServiceConstructionException.getCause()) to find the real failure and the stack location.
  2. Fix the root cause inside the factory method (null guard, resource availability, argument validation).
  3. Ensure all dependencies the method declares as parameters were themselves constructed successfully before this service.
  4. Add logging/defensive checks in the factory so failures are explicit rather than generic NPEs.

Example fix

// before
@DependentService
public MyService build(Config cfg) {
  return new MyService(cfg.getPath().toFile()); // NPE if getPath()==null
}

// after
@DependentService
public MyService build(Config cfg) {
  java.nio.file.Path p = Objects.requireNonNull(cfg.getPath(), "config path");
  return new MyService(p.toFile());
}
Defensive patterns

Strategy: try-catch

Try / catch

try {
  DependentServiceResolver.inject(obj);
} catch (ServiceConstructionException e) {
  Throwable cause = e.getCause();
  Msg.error(this, "Service construction failed via " + e.getMessage(), cause);
  // handle or rethrow after enriching context
}

Prevention

When it happens

Trigger: A @DependentService method throws a NullPointerException, an IOException, an IllegalStateException, or any exception during construction. A dependency passed into the method is in an invalid state. A required resource (file, network, config) is unavailable when the factory runs.

Common situations: Factory methods that open files or databases at construction time; null dependencies due to incomplete registration; constructor logic that validates configuration and rejects bad state; service initialization ordering where a prerequisite is not yet ready.

Related errors


AI-assisted analysis of NationalSecurityAgency/ghidra@d5f144c24d (2026-08-14). Data as JSON: /api/errors/24ad1cc543c61380. Report an issue: GitHub.