【问题标题】:How to re-publish a service property using field assignment in iPOJO如何使用 iPOJO 中的字段分配重新发布服务属性
【发布时间】:2014-10-02 21:35:40
【问题描述】:

我是 iPOJO 的新手。作为学习 iPOJO 框架的一部分,我发现了以下问题。

我正在发布服务属性“when”的服务“ServicePropertiesExample”,初始/默认值设置为 0。此服务属性附加到字段“count”。 当归档的“计数”设置为 null 时,服务属性“何时”未发布(如 iPOJO 文档中http://felix.apache.org/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-userguide/describing-components/providing-osgi-services.html

但是,当从值 null 重新分配字段“计数”时,我得到一个 NullPointerException。我想重新发布此服务属性。这是怎么做到的?

@ServiceProperty(name = "when", value = "0")
private String count;

public ServicePropertiesExample() {
    try {
        SwingUtilities
                .invokeAndWait(() -> {
                    panel = new JPanel();
                    JButton setPropertyValueTo1 = new JButton(
                            "Update Service Properties");
                    setPropertyValueTo1.addActionListener((e) -> {                                                      
                        count = String.valueOf(1);
                        });
                    JButton setPropertyValueToNull = new JButton("Set Property value to null");
                    setPropertyValueToNull.addActionListener((e)->{
                        System.out.println("Setting property value to null");
                        count = null;
                    });
                    panel.add(setPropertyValueTo1);
                    panel.add(setPropertyValueToNull);
                });
    } catch (InvocationTargetException | InterruptedException e) {
        e.printStackTrace();
    }
}

这是一个例外:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  at org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandler.__M_onSet(ProvidedServiceHandler.java:417)
  at org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandler.onSet(ProvidedServiceHandler.java)
  at org.apache.felix.ipojo.InstanceManager.onSet(InstanceManager.java:1401)
  at com.steve.swing.components.ServicePropertiesExample.__setcount(ServicePropertiesExample.java)
  at com.steve.swing.components.ServicePropertiesExample.__M_lambda$1(ServicePropertiesExample.java:49)
  at com.steve.swing.components.ServicePropertiesExample.lambda$1(ServicePropertiesExample.java)
  at com.steve.swing.components.ServicePropertiesExample$$Lambda$3/2135247888.actionPerformed(Unknown Source)
  at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
 ....

【问题讨论】:

    标签: java osgi ipojo


    【解决方案1】:

    您不能将“null”分配给服务属性。尝试使用其他值,例如 0 或 -1。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-08
      • 1970-01-01
      • 2012-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多