Question

I am new to the use of smack library and making one chatting application.when i add friend, from this code..,it works.

public void Addcontact() {    
Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);
Roster roster = m_connection.getRoster();

if(!roster.contains("abc@gmail.com")) {   
 try {           
        roster.createEntry("abc@gmail.com", "abc", null);               
    } catch (XMPPException e) {          
        e.printStackTrace();
    }
}else {
    Log.i("error= ", "contains");
}
}

this code is working for adding single friend..,but now i want to add friend in roster dynamically..,i tried this code

   private void addEntry(String jid, String user, String[] groups) {
    Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);
     roster = commanInstant.connection.getRoster();

        try {           
            roster.createEntry(jid,user,null);               
            System.out.println(""+jid+user);
        } catch (XMPPException e) {          
            e.printStackTrace();
            System.out.println(""+e);
        }
    }
 }

so it is giving me null pointer execption at button click.

here is my logcat

03-31 12:34:19.740: D/AndroidRuntime(4886): Shutting down VM
   03-31 12:34:19.770: E/AndroidRuntime(4886): FATAL EXCEPTION: main
  03-31 12:34:19.770: E/AndroidRuntime(4886): java.lang.NullPointerException
  03-31 12:34:19.770: E/AndroidRuntime(4886):   at   org.jivesoftware.smack.packet.RosterPacket$Item.<init>(RosterPacket.java:117)
  03-31 12:34:19.770: E/AndroidRuntime(4886):   at org.jivesoftware.smack.Roster.createEntry(Roster.java:283)
  03-31 12:34:19.770: E/AndroidRuntime(4886):   at  com.wisechatter.Contact_ListActivity.addEntry(Contact_ListActivity.java:176)
  03-31 12:34:19.770: E/AndroidRuntime(4886):   at co  m.wisechatter.Contact_ListActivity.onClick(Contact_ListActivity.java:164)
  03-31 12:34:19.770: E/AndroidRuntime(4886):   at android.view.View.performClick(View.java:4204)
  03-31 12:34:19.770: E/AndroidRuntime(4886):   at android.view.View$PerformClick.run(View.java:17355)
  03-31 12:34:19.770: E/AndroidRuntime(4886):   at and roid.os.Handler.handleCallback(Handler.java:725)
  03-31 12:34:19.770: E/AndroidRuntime(4886):   at android.os.Handler.dispatchMessage(Handler.java:92)
  03-31 12:34:19.770: E/AndroidRuntime(4886):   at android.os.Looper.loop(Looper.java:137)
  03-31 12:34:19.770: E/AndroidRuntime(4886):   at  android.app.ActivityThread.main(ActivityThread.java:5041)
  03-31 12:34:19.770: E/AndroidRuntime(4886):   at   java.lang.reflect.Method.invokeNative(Native Method)
  03-31 12:34:19.770: E/AndroidRuntime(4886):   at  java.lang.reflect.Method.invoke(Method.java:511)
  03-31 12:34:19.770: E/AndroidRuntime(4886):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  03-31 12:34:19.770: E/AndroidRuntime(4886):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
   03-31 12:34:19.770: E/AndroidRuntime(4886):  at dalvik.system.NativeStart.main(Native Method)

i know it is very silly question,but i am not getting proper solution..,please help me..,thanks In advance..:)

Was it helpful?

Solution

just change your add_account line here

  private void addEntry(String userName) 
{
    try {
        Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);

        String jid=userName+Constants.ADVANTLPC__SMACK;
        commanInstance.roster.createEntry(jid, jid  , null);
        Presence pres = new Presence(Presence.Type.subscribe);
        pres.setFrom(jid);
        commanInstance.connection.sendPacket(pres); 

        commanInstance.loadFriendList();

    } catch (Exception e) {
        Log.e("tag", "unable to add contact: ", e);
    }

}

}

and in loadfriendlist() here will be your list view.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top