code4craft/webmagic · error · IllegalArgumentException
language and script must not be null!
Error message
language and script must not be null!
What it means
The ScriptProcessor constructor requires both a Language and a script string. If either is null it throws IllegalArgumentException "language and script must not be null!" before any engine is created. It is a fail-fast guard against unusable script processor configuration.
Solutions
- Pass a valid Language enum value (check spelling of the config value used to resolve it).
- Ensure the script string is non-null before constructing — load it via ScriptProcessorBuilder.script/scriptFromFile which validate IO errors.
- Add a caller-side null check with a clear message identifying which argument was null.
Example fix
// before new ScriptProcessor(languageConfig, scriptText, 1); // after Objects.requireNonNull(languageConfig, "language"); Objects.requireNonNull(scriptText, "script"); new ScriptProcessor(languageConfig, scriptText, 1);
Defensive patterns
Strategy: validation
Validate before calling
if (language == null) throw new IllegalStateException("Language not resolved from config");
if (script == null || script.trim().isEmpty()) throw new IllegalStateException("Script is empty"); Try / catch
try {
processor = new ScriptProcessor(language, script, threadNum);
} catch (IllegalArgumentException e) {
if (e.getMessage().contains("must not be null")) {
throw new IllegalStateException("language and script are required to build ScriptProcessor", e);
}
throw e;
} Prevention
- Null-check config-derived Language values before constructing
- Default empty script sources to an explicit error instead of passing null
- Use Objects.requireNonNull with named messages at API boundaries
When it happens
Trigger: Calling new ScriptProcessor(null, script, threadNum) or new ScriptProcessor(language, null, threadNum) — e.g. a Language enum value resolved to null from config, or a script string loaded from an empty/missing source and never defaulted.
Common situations: Language loaded from a config file with a typo that resolves to null; a file/classpath script read returning null (or the caller passing a null literal) and forwarded directly to the constructor.
Related errors
- task or site can not be null
- threadNum should be more than one!
- emptySleepTime should be more than zero!
- Illegal HTTP Method
- illegal encoding
AI-assisted analysis of code4craft/webmagic@67816a19d6 (2026-09-08).
Data as JSON: /api/errors/e7630389705ad4af.
Report an issue: GitHub.
Appendix: source
Thrown at webmagic-scripts/src/main/java/us/codecraft/webmagic/scripts/ScriptProcessor.java:34
/**
* @author code4crafter@gmail.com
* @since 0.4.1
*/
public class ScriptProcessor implements PageProcessor {
private ScriptEnginePool enginePool;
private String defines;
private String script;
private final Language language;
private Site site = Site.me();
public ScriptProcessor(Language language, String script, int threadNum) {
if (language == null || script == null) {
throw new IllegalArgumentException("language and script must not be null!");
}
this.language = language;
enginePool = new ScriptEnginePool(language, threadNum);
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(language.getDefineFile());
try {
defines = IOUtils.toString(resourceAsStream, Charset.defaultCharset());
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
this.script = script;
}
@Override
public void process(Page page) {
ScriptEngine engine = enginePool.getEngine();
try {
ScriptContext context = engine.getContext();
context.setAttribute("page", page, ScriptContext.ENGINE_SCOPE);View on GitHub (pinned to 67816a19d6)