Question

I'm trying to implement a chat client for Android which connects to google talk service. I have done most of it, I connect to the server, I get contacts (and show them in a ListView) and I open a new Activity when clicking a contact name on the list where the messages are send and received. My problem is that now I'm trying to get the Presence of the contacts of a user's account. I have written this code but it is not working and I'm not able to discover why. Any help is appreciated.

Button connect = (Button) findViewById(R.id.connect);
    connect.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

            String username = getTheText(R.id.UserName);
            String password = getTheText(R.id.Password);

            login(username, password);
            try {
        Thread.sleep(5000);
    } catch (InterruptedException e1) {
        e1.printStackTrace();
      }
            roster = connection.getRoster();

            entries = roster.getEntries();
            arrayOfEntries = new ArrayList<RosterEntry>(entries);
            searchedContacts = GetSearchContacts();\\This method is the one who is not working correctly

            try{

                launchNextActivity();

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

             }

        }


     });
    /*
 * Asigna los datos del ArrayList de RosterEntry al ArraList de SearchContacts
 */

private ArrayList<SearchContacts> GetSearchContacts(){
    ArrayList<SearchContacts> results = new ArrayList<SearchContacts>();
      \\The class SearchContacts is an own class very simple with a constructor which assigns 4 parameters to the objects of the class, and getters and setters for every parameter.
    for (RosterEntry r : arrayOfEntries){
        SearchContacts sc = new SearchContacts(null, null,null,null);
        sc.setNick(r.getName());
        sc.setEmail(r.getUser());
        roster.setSubscriptionMode(Roster.SubscriptionMode.accept_all);
        prs = roster.getPresence(r.getUser());
        Presence.Mode presence2 = prs.getMode();
        status = ConvierteEnumToString(presence2);
        sc.setFirstSentence(status);
        results.add(sc);


    }
    return results;

}

\*
     *
     *\ 
private void ConvierteEnumToString(Presence.Mode pm){

    switch(pm){
    case available:
        Log.i(LOGTAG,"Case Available");
        status= "Available";
        break;
    case away:
        Log.i(LOGTAG,"Case away");
        status= "Away";
        break;
    case chat:
        Log.i(LOGTAG,"Case chat");
        status= "chat";
        break;
    case dnd:
        Log.i(LOGTAG,"Case dnd");
        status= "Do not disturb";
        break;
    case xa:
        Log.i(LOGTAG,"Case xa");
        status= "Unavailable";
                    break;
            default:
                    Log.i(LOGTAG,"Invalid status");
                    status="Invalid";
                    break;

    }




}

If I change the attribute of this line: String status = ConvierteEnumToString(presence2); and I put directly Presence.Mode.available, it assigns the status available to all of my contacts but it works correctly. If I let it as it is in the code above I get an Exception like this:

04-10 23:11:02.866: W/dalvikvm(1722): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-10 23:11:02.886: E/AndroidRuntime(1722): FATAL EXCEPTION: main
04-10 23:11:02.886: E/AndroidRuntime(1722): java.lang.NullPointerException
04-10 23:11:02.886: E/AndroidRuntime(1722):     at android.chat.Login.GetSearchContacts(Login.java:164)
04-10 23:11:02.886: E/AndroidRuntime(1722):     at android.chat.Login.access$1(Login.java:153)
04-10 23:11:02.886: E/AndroidRuntime(1722):     at android.chat.Login$1.onClick(Login.java:81)
04-10 23:11:02.886: E/AndroidRuntime(1722):     at android.view.View.performClick(View.java:2485)
04-10 23:11:02.886: E/AndroidRuntime(1722):     at android.view.View$PerformClick.run(View.java:9080)
04-10 23:11:02.886: E/AndroidRuntime(1722):     at android.os.Handler.handleCallback(Handler.java:587)
04-10 23:11:02.886: E/AndroidRuntime(1722):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-10 23:11:02.886: E/AndroidRuntime(1722):     at android.os.Looper.loop(Looper.java:123)
04-10 23:11:02.886: E/AndroidRuntime(1722):     at android.app.ActivityThread.main(ActivityThread.java:3683)
04-10 23:11:02.886: E/AndroidRuntime(1722):     at java.lang.reflect.Method.invokeNative(Native Method)
04-10 23:11:02.886: E/AndroidRuntime(1722):     at java.lang.reflect.Method.invoke(Method.java:507)
04-10 23:11:02.886: E/AndroidRuntime(1722):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-10 23:11:02.886: E/AndroidRuntime(1722):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-10 23:11:02.886: E/AndroidRuntime(1722):     at dalvik.system.NativeStart.main(Native Method)

With the difference that I sometimes get One user status and then the Exception occurs and sometimes no status is got at all. Thank you very much for your help!

Was it helpful?

Solution

I have discovered the problem. I add the solution here to whom it may concern.

The problem was that when this line is executed:

Presence.Mode presence2 = prs.getMode();

one of the possible values that presence2 can get is null. So you are passing a null variable to a try/catch structure which was waiting for a Presence.Mode presence variable... Thats why a NullPointerException is got.

I have added an if clause just after that line which test if the variable presence2 is equals to null and if it happens it changes it somehow. I suppose it can also be done surrounding the line with a try/catch structure and treating the NullPointerException somehow.

I hope it can help to anyone at least to not waste time thinking a solution for this!

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