连接到 Mqtt 在线代理时出现 NullPointerException

NullPointerException while connecting to Mqtt on-line broker

我正在尝试使用下面的代码和 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? 上网none教程或攻略很有用,我已经下载了mosquitto.org.crtSSL连接文件,但不知道在哪里使用,也没找到教程

编辑
将 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);

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

Mosquitto 的 URI 需要协议。查看它的源代码,这是抛出异常的地方,class 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";