triggerJSEvent fails with null object reference

I’m trying to dispatch an event from native java code to javascript environments to trigger some javascript functions. I tried using triggerJSEvent but to no success. I tried the official documentation, code from forum but they all won’t work.

The code:

@CapacitorPlugin(name = "TestEvent")
public class TestEvent extends Plugin {
    Bridge myBridge;

    public void load() {
        myBridge = bridge;

    public void testCall() {
        The problematic part:

        // A. As-is from official documentation (
        //bridge.triggerJSEvent("testEvent", "window", "{ 'testKey': 'dataValue' }");

        // B. Creating a copy and referencing bridge from it (
        myBridge.triggerJSEvent("testEvent", "window", "{ 'testKey': 'dataValue' }");

Here’s the error log:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.getcapacitor.Bridge.triggerJSEvent(java.lang.String, java.lang.String, java.lang.String)' on a null object reference
    at com.capacitor.reproduction.triggerJSEvent.TestEvent.testCall(
    at com.capacitor.reproduction.triggerJSEvent.TestPlugin.testRun(
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.getcapacitor.PluginHandle.invoke( 
    at com.getcapacitor.Bridge.lambda$callPluginMethod$0( 
    at com.getcapacitor.Bridge.$r8$lambda$ehFTi5f4HhVNFKTbCKAYDkpQYRA(Unknown Source:0) 
    at com.getcapacitor.Bridge$$ Source:8) 
    at android.os.Handler.handleCallback( 
    at android.os.Handler.dispatchMessage( 
    at android.os.Looper.loop( 

More information can be found in the bug report I created: bug: Capacitor listeners failing to be called from Android notifyListeners with foreground service · Issue #6234 · ionic-team/capacitor · GitHub

It seems like there’s a bug with that function or official documentation is wrong or outdated.
I literally searched everywhere to search for some example codes, but I couldn’t find much. These are some codes(reference) I’ve tried:

Am I doing something wrong? Or is there a better/alternative way of calling a javascript function from native java code?