我正在尝试将Java ME App Client连接到MQTT代理。 我能够创建一个客户端,交叉检查客户端服务器URI 但是当我尝试连接时,我看到异常:
以下是代码:
try {
myClient = new MqttClient(BROKER_URL, MqttClient.generateClientId()); //, new MemoryPersistence());
if(myClient == null)
{
System.out.println("myClient = NULL");
return;
}
System.out.println("ServerURI: "+myClient.getServerURI());
System.out.println("is Connected: "+myClient.isConnected());
System.out.println("Setting Call back");
myClient.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable cause) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
// String payload = new String(message.getPayload());
System.out.println(topic+ "--->"+ new String(message.getPayload()));
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
System.out.println("Trying to Connect to BROKER");
myClient.connect();
// myClient.connect(connOpt);
System.out.println("Subscribing to WORLD TOPIC");
myClient.subscribe("world");
} catch (MqttException e) {
System.out.println("--- Caught Exception ---");
// System.out.println("Exception: "+e.toString());
e.printStackTrace();
System.exit(-1);
return;
}
我在连接上得到并且异常。这是例外:
java.lang.NullPointerException
- org/eclipse/paho/client/mqttv3/internal/MessageCatalog.getMessage(MessageCatalog.java:46)
- org/eclipse/paho/client/mqttv3/MqttException.getMessage(MqttException.java:200)
- org/eclipse/paho/client/mqttv3/MqttException.toString(MqttException.java:208)
- java/lang/String.valueOf(), bci=11
- java/io/PrintStream.print(), bci=2
- java/io/PrintStream.println(), bci=2
- java/lang/Throwable.printStackTrace(), bci=2
- java/lang/Throwable.printStackTrace(), bci=4
- javameapp82/SimpleMqttClient.testMQTTClient(SimpleMqttClient.java:96)
- javameapp82/JavaMEApp82.startApp(JavaMEApp82.java:50)
- javax/microedition/midlet/MIDletTunnelImpl.callStartApp(), bci=1
- com/sun/midp/midlet/MIDletPeer.startApp(), bci=5
- com/sun/midp/midlet/MIDletStateHandler.startSuite(), bci=264
- com/sun/midp/main/AbstractMIDletSuiteLoader.startSuite(), bci=38
- com/sun/midp/main/CldcMIDletSuiteLoader.startSuite(), bci=5
- com/sun/midp/main/AbstractMIDletSuiteLoader.runMIDletSuite(), bci=132
- com/sun/midp/main/AppIsolateMIDletSuiteLoader.main(), bci=26
不知道为什么我会得到这个例外,谢谢你的帮助。