I am trying to implement the google's sample for wifidirect connection. Everyhing works ok but on the option menu if I click on disconnect the app crashes with 'nullpointer exception'. The error is shown on the line 'mconnecion.teardown()';
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: // using app icon for navigation up or home:
Log.d(TAG, "home clicked.");
// startActivity(new Intent(home.class, Intent.FLAG_ACTIVITY_CLEAR_TOP));
return true;
case R.id.atn_direct_enable:
startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
return true;
case R.id.atn_direct_discover:
listFragment.onInitiateDiscovery();
Log.d(TAG, "onOptionsItemSelected : start discoverying ");
discoverPeers();
return true;
case R.id.disconnect:
Log.d(TAG, "onOptionsItemSelected : disconnect all connections and stop server ");
mConnection.tearDown();
mConnection=null;
return true;
default:
return super.onOptionsItemSelected(item);
}
}
'mconnection' is a class and 'teardown()' method is like following
public void tearDown() {
mChatServer.tearDown();
mChatClient.tearDown();
isReady = false;
mContext = null;
}
How can i solve this problem?
Its showing null pointer exception because the object is null there. check if the object is null and the problem should not exist.
if (mConnection!=null)
mConnection.tearDown();
mConnection=null;
return true;
This should solve your problem i hope