Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[paho-dev] Null Pointer exception.- Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference

Hi,


I have developed an android application which connects to MQTT server on tls/ssl. 
There is one MQTTSubscriber class. 

MQTTSubscriber works great on wi-fi connections but when connecting with ActiveMQ (ver 5.14.3) using MQTT Library ver 3 it throws the following exception. I am using Android ver 7.0 on LG G6.

The exception is as follows:

AlertMqttSubscriber , java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at java.net.URI$Parser.parse(URI.java:3050)
at java.net.URI.<init>(URI.java:590)
at org.eclipse.paho.client.mqttv3.MqttConnectOptions.validateURI(MqttConnectOptions.java:528)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:463)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:328)
at org.eclipse.paho.android.service.MqttConnection.connect(MqttConnection.java:289)
at org.eclipse.paho.android.service.MqttService.connect(MqttService.java:329)
at org.eclipse.paho.android.service.MqttAndroidClient.doConnect(MqttAndroidClient.java:467)
at org.eclipse.paho.android.service.MqttAndroidClient.access$200(MqttAndroidClient.java:76)
at org.eclipse.paho.android.service.MqttAndroidClient$MyServiceConnection.onServiceConnected(MqttAndroidClient.java:115)
at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1535)
at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1563)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6316)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)


The Java code for respective exception is as follows:

mySSLContext.init(null, myWrappedTrustManagers, null);
// Create SSLSocketFactory
SSLSocketFactory myFactory = mySSLContext.getSocketFactory();
MqttConnectOptions myOptions = new MqttConnectOptions();
myOptions.setCleanSession(false);
myOptions.setAutomaticReconnect(true);
myOptions.setKeepAliveInterval(Constant.KEEP_ALIVE_INTERVAL);
myOptions.setSocketFactory(myFactory);
theMQTTClient = new MqttAndroidClient(this.theContext,aBrokerUrl,aDeviceId);
IMqttToken token = theMQTTClient.connect(myOptions);
Exception e =token.getException();
token.setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
try {
//logging connection status = true
ArrayList<String> myTopicName = getTopicName(aContext);
for (int topicName=0;topicName<myTopicName.size();topicName++) {
theMQTTClient.subscribe(myTopicName.get(topicName), 1);
}
} catch (MqttException exception) {
//logging exception
}
}

@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
//logging exception
}
});
theMQTTClient.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable cause) {
//logging exception
}

Can anyone guide me a little more about it. What would be the reason why I am getting such exception and h
how can these be handled or removed?

Kind Regards,
Sumayya Shahzad

Back to the top