OSGI EventAdmin ไม่สามารถส่งเหตุการณ์ได้ - รับข้อยกเว้นจุดว่างใน Producer

ฉันกำลังพยายามส่งกิจกรรมไปยังผู้บริโภค ฉันได้รับ NullPointerException

นี่คือรหัสเหตุการณ์ของฉัน

public class Activator implements BundleActivator {

    EventAdmin eventAdmin;
    @Override
    public void start(BundleContext bundleContext) throws Exception {
        System.out.println("started");
        Dictionary<String, String> properties = new Hashtable<>();
        properties.put("test", "blah");
        Event event = new Event("test/sent", properties);
        System.out.println("sending");
        eventAdmin.sendEvent(event);
    }

    @Override
    public void stop(BundleContext bundleContext) throws Exception {
        System.out.println("stopped ");
    }
} 

นี่คือบันทึก

คุณใช้ฟิลด์ eventAdmin ที่ไม่เคยตั้งค่าไว้ ดังนั้นคุณจะได้รับ NullPointerException ฉันขอแนะนำให้ใช้บริการที่ประกาศแทนและฉีด EventAdmin โดยใช้ @Reference


person TheTechGuy    schedule 26.08.2019    source แหล่งที่มา


คำตอบ (1)


ดู ผู้ดูแลกิจกรรม OSGi – เผยแพร่และสมัครสมาชิก จาก Vogella สำหรับรายละเอียดทั้งหมด

org.osgi.framework.BundleException: ข้อยกเว้นใน com.aml.project.Activator.start() ของบันเดิล h ที่ org.eclipse.osgi.internal.framework.BundleContextImpl.startActivator(BundleContextImpl.java:863) ที่ org.eclipse.osgi.internal.framework.BundleContextImpl.start(BundleContextImpl.java:791) ที่ org.eclipse.osgi.internal .framework.EquinoxBundle.startWorker0(EquinoxBundle.java:1013) ที่ org.eclipse.osgi.internal.framework.EquinoxBundle$EquinoxModule.startWorker(EquinoxBundle.java:365) ที่ org.eclipse.osgi.container.Module.doStart(โมดูล .java:598) ที่ org.eclipse.osgi.container.Module.start(Module.java:462) ที่ org.eclipse.osgi.internal.framework.EquinoxBundle.start(EquinoxBundle.java:439) ที่ org.apache felix.gogo.command.Basic.start(Basic.java:739) ที่ java.base/jdk.internal.reflect.NativeMethodAccessorImpl.inurge0(Native Method) ที่ java.base/jdk.internal.reflect.NativeMethodAccessorImpl.inurge(NativeMethodAccessorImpl .java:62) ที่ java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.inurge(DelegatingMethodAccessorImpl.java:43) ที่ java.base/java.lang.reflect.Method.inrigg(Method.java:567) ที่ org apache.felix.gogo.runtime.Reflective.inrigg(Reflective.java:139) ที่ org.apache.felix.gogo.runtime.CommandProxy.execute(CommandProxy.java:91) ที่ org.apache.felix.gogo.runtime การปิด 415) ที่ org.apache.felix.gogo.runtime.Pipe.doCall(Pipe.java:416) ที่ org.apache.felix.gogo.runtime.Pipe.call(Pipe.java:229) ที่ org.apache.felix .gogo.runtime.Pipe.call(Pipe.java:59) ที่ java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ที่ java.base/java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1128) ที่ java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ที่ java.base/java.lang.Thread.run(Thread.java:835) โดย: java.lang.NullPointerException ที่ com.aml.project.Activator.start(Activator.java:21) ที่ org.eclipse.osgi.internal.framework.BundleContextImpl$3.run(BundleContextImpl.java:842) ที่ org.eclipse .osgi.internal.framework.BundleContextImpl$3.run(BundleContextImpl.java:1) ที่ java.base/java.security.AccessController.doPrivileged(AccessController.java:552) ที่ org.eclipse.osgi.internal.framework BundleContextImpl.startActivator (BundleContextImpl.java:834) ... 23 เพิ่มเติม java.lang.NullPointerException

person Christian Schneider    schedule 27.08.2019