Justson/AgentWeb · error · Exception

injected name can not be null

Error message

injected name can not be null

What it means

The JsCallJava constructor throws when the JavaScript interface name passed in is empty or null (checked via TextUtils.isEmpty). This name is the window-level object name used by injected JS, so it is mandatory. The raw Exception is caught/wrapped internally into the library's JS-bridge setup failure path.

Solutions

  1. Pass a non-empty interface name, e.g. new JsCallJava(obj, "androidBridge")
  2. Validate the name string before constructing
  3. If the name comes from config, provide a default and check emptiness at load time
  4. Prefer AgentWeb's higher-level addJavascriptInterface APIs which handle naming

Example fix

// before
JsCallJava js = new JsCallJava(bridge, mName); // mName may be ""
// after
if (!TextUtils.isEmpty(mName)) {
    JsCallJava js = new JsCallJava(bridge, mName);
}
Defensive patterns

Strategy: validation

Validate before calling

if (name == null || name.trim().isEmpty()) { throw new IllegalArgumentException("JS interface name required"); }

Type guard

boolean validJsName(String n) { return n != null && n.matches("[A-Za-z_$][A-Za-z0-9_$]*"); }

Try / catch

try { new JsCallJava(obj, name); } catch (Exception e) { Log.e(TAG, "invalid JS interface name", e); }

Prevention

When it happens

Trigger: Calling new JsCallJava(interfaceObj, null) or with an empty string name; in AgentWeb usage, registering a Java object via the JS-injection APIs with a null/empty interface name.

Common situations: Building the JS bridge programmatically with a name read from config/remote data that is missing, typos where the name variable defaults to empty, or calling low-level JsCallJava directly instead of through SafeJsInterface/JavascriptInterfaceHolder helpers.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of Justson/AgentWeb@8f7f6adbf0 (2026-09-11). Data as JSON: /api/errors/0760d9ffc286747e. Report an issue: GitHub.

Appendix: source

Thrown at agentweb-core/src/main/java/com/just/agentweb/JsCallJava.java:47

public class JsCallJava {
    private final static String TAG = "JsCallJava";
    private final static String RETURN_RESULT_FORMAT = "{\"CODE\": %d, \"result\": %s}";
    private static final String MSG_PROMPT_HEADER = "AgentWeb:";
    private static final String KEY_OBJ = "obj";
    private static final String KEY_METHOD = "method";
    private static final String KEY_TYPES = "types";
    private static final String KEY_ARGS = "args";
    private static final String[] IGNORE_UNSAFE_METHODS = {"getClass", "hashCode", "notify", "notifyAll", "equals", "toString", "wait"};
    private HashMap<String, Method> mMethodsMap;
    private Object mInterfaceObj;
    private String mInterfacedName;
    private String mPreloadInterfaceJs;

    public JsCallJava(Object interfaceObj, String interfaceName) {
        try {
            if (TextUtils.isEmpty(interfaceName)) {
                throw new Exception("injected name can not be null");
            }
            mInterfaceObj = interfaceObj;
            mInterfacedName = interfaceName;
            mMethodsMap = new HashMap<String, Method>();
            // getMethods会获得所有继承与非继承的方法
            Method[] methods = mInterfaceObj.getClass().getMethods();
            // 拼接的js脚本可参照备份文件:./library/doc/injected.js
            StringBuilder sb = new StringBuilder("javascript:(function(b){console.log(\"");
            sb.append(mInterfacedName);
            sb.append(" init begin\");var a={queue:[],callback:function(){var d=Array.prototype.slice.call(arguments,0);var c=d.shift();var e=d.shift();this.queue[c].apply(this,d);if(!e){delete this.queue[c]}}};");
            for (Method method : methods) {
                Log.i("Info","method:"+method);
                String sign;
                if ((sign = genJavaMethodSign(method)) == null) {
                    continue;
                }
                mMethodsMap.put(sign, method);
                sb.append(String.format("a.%s=", method.getName()));

View on GitHub (pinned to 8f7f6adbf0)