0

I am new to Paho android Service and MQTT protocol. I have been trying to write a simple code that connects to the mqtt broker through "xx.xx.xx.xxx:1883". When i run the below posted code, it generates the logcat output.

why i am getting this NPE?

note:

I have the mosquitto server installed

line_35

client.connect(mContext, new IMqttActionListener() {

Code:

private Context mContext;
private final String serverURI = "xx.xx.xx.xx:1883";
private final String clientID = MqttClient.generateClientId();
private MqttAndroidClient client = null;
private final String TAG = this.getClass().getSimpleName();
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mqtt__proj_00_layout);

        MqttAndroidClient client = new MqttAndroidClient(mContext, serverURI, clientID);
        if (client != null) {
            try {
                client.connect(mContext, new IMqttActionListener() {

                    @Override
                    public void onSuccess(IMqttToken arg0) {
                        // TODO Auto-generated method stub
                        Log.i(TAG, "Connection Successful.");
                    }

                    @Override
                    public void onFailure(IMqttToken arg0, Throwable arg1) {
                        // TODO Auto-generated method stub
                        Log.i(TAG, "Connection Failed.");
                    }
                });
            } catch (MqttException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

}

logcat:

11-13 10:51:07.913: E/AndroidRuntime(26656): FATAL EXCEPTION: main
11-13 10:51:07.913: E/AndroidRuntime(26656): Process: com.example.mqtt_proj_00, PID: 26656
11-13 10:51:07.913: E/AndroidRuntime(26656): java.lang.RuntimeException: Unable to start activity  
ComponentInfo{com.example.mqtt_proj_00/com.example.mqtt_proj_00.MQTT_Proj_00}:   
java.lang.NullPointerException
11-13 10:51:07.913: E/AndroidRuntime(26656):    at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
11-13 10:51:07.913: E/AndroidRuntime(26656):    at   
com.example.mqtt_proj_00.MQTT_Proj_00.onCreate(MQTT_Proj_00.java:35)
Amrmsmb
  • 1
  • 27
  • 104
  • 226

1 Answers1

1

mContext is null, you need to initialize it.

You can do so by replacing mContext (if you're not using it elsewhere) :

client.connect(getApplicationContext(), new IMqttActionListener(){...});
2Dee
  • 8,609
  • 7
  • 42
  • 53
  • thank you or the answer. now after using "getapplicatincontext" the console says " Could not find org.eclipse.paho.android.service.apk" – Amrmsmb Nov 13 '14 at 10:23
  • Does this help ? http://stackoverflow.com/questions/4778113/android-eclipse-could-not-find-apk – 2Dee Nov 13 '14 at 10:43
  • thanka lot, the problem is solved. but when I comment out the if-statement mentioned in the code posted, the App does not crash. but whem I use the "if (clien != null)" the app crashes and logcat displays nothin. should i post anew question for that? – Amrmsmb Nov 13 '14 at 11:02
  • I would suggest creating a new question, yes. If LogCat indeed displays nothing, you could set it to verbose and see if you can more detailled logs that way. – 2Dee Nov 13 '14 at 12:25