【问题标题】:NullPointerException while connecting to Mqtt on-line broker连接到 Mqtt 在线代理时出现 NullPointerException
【发布时间】:2019-03-05 13:25:44
【问题描述】:

我正在尝试使用下面的代码和 Java 中的 Paho 库连接到在线经纪人 https://test.mosquitto.org/

private final String brokerURI = "test.mosquitto.org:1883"; //should be changed to 8883 with SSL
try { //tentativo di creazione del client
        client = new MqttClient(brokerURI, idClient); <--NullPointerException here
        client.setCallback(new ClientCallback(codaTopic, codaMessaggi, finestra)); //set delle callback
        setConnectionOptions(); //set delle opzioni connessione
        client.connect(opzioni); //connessione al server
    } catch (MqttException e) {
        System.err.println(e.getMessage());
        System.err.println("Connessione fallita Client, riavviare il sistema.");
    }

在此处设置连接选项:

    private void setConnectionOptions() {
    opzioni = new MqttConnectOptions();
    opzioni.setAutomaticReconnect(true);
    opzioni.setCleanSession(false);
    opzioni.setConnectionTimeout(30);
    opzioni.setKeepAliveInterval(60);
}

但它在创建MqttClient 时继续显示NullPointerException。特别是控制台显示:

Exception in thread "Thread-3" java.lang.NullPointerException
at org.eclipse.paho.client.mqttv3.MqttConnectOptions.validateURI(MqttConnectOptions.java:489)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:291)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:185)
at org.eclipse.paho.client.mqttv3.MqttClient.<init>(MqttClient.java:226)
at org.eclipse.paho.client.mqttv3.MqttClient.<init>(MqttClient.java:138)
at client.Client.run(Client.java:78)

如何连接和使用 SSL? 网上冲浪没有教程或指南有用,我已经下载了用于 SSL 连接的mosquitto.org.crt文件,但我不知道在哪里使用它,我没有找到教程。

编辑
将 BrokerUri 更改为
private final String brokerURI = "tcp://test.mosquitto.org:1883"; //indirizzo broker 控制台显示错误

Client non connesso (32104)
at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:31)
at org.eclipse.paho.client.mqttv3.internal.ClientComms.sendNoWait(ClientComms.java:166)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.subscribe(MqttAsyncClient.java:835)
at org.eclipse.paho.client.mqttv3.MqttClient.subscribe(MqttClient.java:322)
at org.eclipse.paho.client.mqttv3.MqttClient.subscribe(MqttClient.java:315)
at client.Client.subscribe(Client.java:214)
at client.Client.run(Client.java:89)

在尝试使用指令订阅主题时

client.subscribe(topic, 1);

主题参数是一个包含主题名称的字符串。

【问题讨论】:

  • idClient 是什么?
  • idCLient 是一个简单的字符串,通常是“mario”之类的名称或类似名称

标签: java mqtt mosquitto paho


【解决方案1】:

Mosquitto 的 URI 需要协议。看看它的源代码,这是你的异常被抛出的地方,MqttConnectOpts.java 类:

protected static int validateURI(String srvURI) {
    try {
        URI vURI = new URI(srvURI);
        if (!vURI.getPath().equals("")) {
            throw new IllegalArgumentException(srvURI);
        }
        if (vURI.getScheme().equals("tcp")) {
            return URI_TYPE_TCP;
        }
        else if (vURI.getScheme().equals("ssl")) {
            return URI_TYPE_SSL;
        }
        else if (vURI.getScheme().equals("local")) {
            return URI_TYPE_LOCAL;
        }
        else {
            throw new IllegalArgumentException(srvURI);
        }
    } catch (URISyntaxException ex) {
        throw new IllegalArgumentException(srvURI);
    }
}

因此,它接受 3 种类型的协议前缀:tcp, ssl, local。关于您的示例,您可以这样尝试:

  • TCP

     private final String brokerURI = "tcp://test.mosquitto.org:1883";
    
  • SSL

    private final String brokerURI = "ssl://test.mosquitto.org:8883";
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 2021-10-21
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多