karatelabs/karate · error · NullPointerException

object is null

Error message

object is null

What it means

ExternalBridge.forInstance wraps a Java object for JS access and rejects a null argument with a plain NullPointerException. The bridge cannot create a JavaObject view of a nonexistent instance, so null is never accepted.

Solutions

  1. Ensure the object passed to forInstance (or into the interop call) is non-null before the call
  2. Null-check the Java value at the call site and handle null in JS rather than crossing the bridge
  3. Trace where the null came from — often an earlier API returned null instead of an object
  4. If null is legitimate, avoid bridging and handle it natively

Example fix

// before
ExternalAccess access = bridge.forInstance(obj); // NPE when obj == null
// after
if (obj != null) {
  ExternalAccess access = bridge.forInstance(obj);
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (obj == null) throw new IllegalArgumentException("expected a Java instance");

Type guard

boolean isBridgable(Object o) { return o != null; }

Prevention

When it happens

Trigger: Calling forInstance(null), or passing a JS-side variable that is actually null/undefined into an API that routes Java objects through ExternalBridge.forInstance.

Common situations: JS expressions like `javaObj.someMethod()` where javaObj resolved to null; interop helpers receiving an uninitialized variable; configuration that expected a Java instance but produced null.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/856c9af971196ff2. Report an issue: GitHub.

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/js/ExternalBridge.java:38

 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package io.karatelabs.js;

public interface ExternalBridge {

    default ExternalAccess forType(String className) {
        try {
            return new JavaType(className);
        } catch (Exception e) {
            return null;
        }
    }

    default ExternalAccess forInstance(Object object) {
        if (object == null) {
            throw new NullPointerException("object is null");
        }
        return new JavaObject(object);
    }

}

View on GitHub (pinned to a22eb90246)