0

I have a Smarthome graduation project and it uses MQTT protocol to communicates with the cloud and an android application that controls it.

I'm trying to create one MqttAndroidClient only and use it in all the activities and that doesn't work and I get an error when I try to pass the same client to a publish or subscribe methods in another class

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.badr.said.muhammad.smarthome/com.badr.said.muhammad.smarthome.Welcome}: java.lang.NullPointerException: Attempt to invoke virtual method 'org.eclipse.paho.client.mqttv3.IMqttDeliveryToken org.eclipse.paho.android.service.MqttService.publish(java.lang.String, java.lang.String, byte[], int, boolean, java.lang.String, java.lang.String)' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)

this is the code where the error occurs

public class Welcome extends AppCompatActivity {

MqttAndroidClient client;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome);

    client = new MqttAndroidClient(this.getApplicationContext(),mqtthost,clientId);
    MqttConnectOptions options = new MqttConnectOptions();
    options.setUserName(username);
    options.setPassword(password.toCharArray());

    try {
        IMqttToken token = client.connect(options);
        token.setActionCallback(new IMqttActionListener() {
            @Override
            public void onSuccess(IMqttToken asyncActionToken) {
                Toast.makeText(Welcome.this, "Connected !", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                Toast.makeText(Welcome.this, "Connection Field , Check Your Internet Network !", Toast.LENGTH_LONG).show();

            }
        });

    } catch (MqttException e) {
        e.printStackTrace();
    }

    pub("Smarthome","check",client);
    sub5(client);

but is the same pub and sub5 methods on some toggle buttons passing MqttAndroidClient as parameter and it works like this code

toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                pub("Smarthome", "devicegon1", client);
            } else {
                pub("Smarthome", "devicegoff1", client);
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            sub();
        }
    });

so what is wrong and any Ideas or help?

Mohammed Adel
  • 177
  • 1
  • 3
  • 10

1 Answers1

1

You are calling pub() before onSuccess() has been called so your not actually connected yet when you try and publish.

Move pub() and sub5() to in that callback and things should improve

hardillb
  • 54,545
  • 11
  • 67
  • 105