Question

i am making an chatting application.,by using "xmpp server" .in my application.i am using a listview in which list of all contacts are showing.,i am trying to handle roster value in thread ,without using thread and handler its working perfectly..,but when

i am using my listdata in Thread..,its giving me "Null Pointer Exception",what i do..i am so confuse..,pleas ehelp me..,here is my code

public class Contact_ListActivity extends Activity  {
TextView tv_header;
TextView tv_account;
TextView tv_contact;
TextView tv_number;
EditText idnumber;
Button add;
 public ListView list_view;
XMPPObjectPass commanInstant;

public ArrayList<ProfileInfo> list = new  ArrayList<ProfileInfo>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.contact_list);

    commanInstant = (XMPPObjectPass)getApplicationContext();

    tv_account=(TextView)findViewById(R.id.tv_account);
    tv_contact=(TextView)findViewById(R.id.tv_contact);
    tv_number=(TextView)findViewById(R.id.tv_number);
    tv_header=(TextView)findViewById(R.id.tv_header);
    idnumber=(EditText)findViewById(R.id.idnumber);

    add=(Button)findViewById(R.id.add);
    list_view=(ListView)findViewById(R.id.listview);

    list_view.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
            //              Object o = list_view.getItemAtPosition(position);
            //              list_activity_bean obj_itemDetails = (list_activity_bean)o;
            //              Toast.makeText(Contact_ListActivity.this, "You have chosen : " + " " + obj_itemDetails.getName(), Toast.LENGTH_LONG).show();
        }  
    });
    tv_account.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent i=new Intent(Contact_ListActivity.this,Account_Question_Activity.class);
            startActivity(i);
        }
    });
}
@Override
protected void onResume()
{
    // TODO Auto-generated method stub
    super.onResume();
    list.clear();
    friendslist();
    list_view.setAdapter(new ListActivityAdapter(this, list));
}

public void friendslist()
{
Thread  coThread = new Thread();
{
    new Runnable() {
        public void run() {

        try
        {

            Roster roster = commanInstant.connection.getRoster();
            Collection<RosterEntry> entries = roster.getEntries();
            tv_number.setText(""+entries.size());
            for(RosterEntry r:entries)
            {
                VCard card = new VCard();
                try {
                    card.load(commanInstant.connection,r.getUser());
                    System.out.println("<-- first anme " + card.getFirstName());

                    //card.get
                    //              System.out.println("<-- getEmailHome anme " + card.getEmailHome());
                    //              System.out.println("<-- getEmailWork anme " + card.getEmailWork());
                    //              System.out.println("<-- getFrom anme " + card.getFrom());
                    //              System.out.println("<-- getJabberId anme " + card.getJabberId());
                    //              System.out.println("<-- getLastName anme " + card.getLastName());
                    //              System.out.println("<-- getMiddleName anme " + card.getMiddleName());
                    //              System.out.println("<-- getNickName anme " + card.getNickName());
                    //              System.out.println("<-- getOrganization anme " + card.getOrganization());

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


                byte[] imgs = card.getAvatar();
                if (imgs != null) {
                    int len = imgs.length;
                    Bitmap img = BitmapFactory.decodeByteArray(imgs, 0, len);

                    list.add(new ProfileInfo( card.getFirstName(),img,"+1PTS"));
                    System.out.println(r);

                }
            }

        } 
        catch (Exception e) 
        {
            e.printStackTrace();
            System.out.println(""+e);
        }
    }
};
coThread.start();
}
}
   }

Thanks in Advance..:)

Was it helpful?

Solution

try this...,it will work...

public class Contact_ListActivity extends Activity  {
TextView tv_header;
TextView tv_account;
TextView tv_contact;
TextView tv_number;
EditText idnumber;
Button add;
 public ListView list_view;
XMPPObjectPass commanInstant;
Handler mHandler;
 public     ArrayList<ProfileInfo> list = new  ArrayList<ProfileInfo>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.contact_list);

    commanInstant = (XMPPObjectPass)getApplicationContext();

    tv_account=(TextView)findViewById(R.id.tv_account);
    tv_contact=(TextView)findViewById(R.id.tv_contact);
    tv_number=(TextView)findViewById(R.id.tv_number);
    tv_header=(TextView)findViewById(R.id.tv_header);
    idnumber=(EditText)findViewById(R.id.idnumber);

    add=(Button)findViewById(R.id.add);
    list_view=(ListView)findViewById(R.id.listview);

    list_view.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
            //              Object o = list_view.getItemAtPosition(position);
            //              list_activity_bean obj_itemDetails = (list_activity_bean)o;
            //              Toast.makeText(Contact_ListActivity.this, "You have chosen : " + " " + obj_itemDetails.getName(), Toast.LENGTH_LONG).show();
        }  
    });
    tv_account.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent i=new Intent(Contact_ListActivity.this,Account_Question_Activity.class);
            startActivity(i);
        }
    });
}
@Override
protected void onResume()
{
    // TODO Auto-generated method stub
    super.onResume();
    list.clear();
    friendslist();
    list_view.setAdapter(new ListActivityAdapter(this, list));
}

public void friendslist()
{
    mHandler = new Handler();
Thread  coThread = new Thread();
{

        mHandler.post(new Runnable() {
              @Override
        public void run() {

        try
        {

            Roster roster = commanInstant.connection.getRoster();
            Collection<RosterEntry> entries = roster.getEntries();
            tv_number.setText(""+entries.size());
            for(RosterEntry r:entries)
            {
                VCard card = new VCard();
                try {
                    card.load(commanInstant.connection,r.getUser());
                    System.out.println("<-- first anme " + card.getFirstName());

                    //card.get
                    //              System.out.println("<-- getEmailHome anme " + card.getEmailHome());
                    //              System.out.println("<-- getEmailWork anme " + card.getEmailWork());
                    //              System.out.println("<-- getFrom anme " + card.getFrom());
                    //              System.out.println("<-- getJabberId anme " + card.getJabberId());
                    //              System.out.println("<-- getLastName anme " + card.getLastName());
                    //              System.out.println("<-- getMiddleName anme " + card.getMiddleName());
                    //              System.out.println("<-- getNickName anme " + card.getNickName());
                    //              System.out.println("<-- getOrganization anme " + card.getOrganization());

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


                byte[] imgs = card.getAvatar();
                if (imgs != null) {
                    int len = imgs.length;
                    Bitmap img = BitmapFactory.decodeByteArray(imgs, 0, len);

                    list.add(new ProfileInfo( card.getFirstName(),img,"+1PTS"));
                    System.out.println(r);

                }
            }

        } 
        catch (Exception e) 
        {
            e.printStackTrace();
            System.out.println(""+e);
        }
    }
});
coThread.start();
}
}
 }

OTHER TIPS

This is because you are trying to do some things like setting text to textViews inside your Thread. All of this should be used only in The MainUIThread. Use Handler.post for everything you want to display in your Activity

@Override protected void onResume()

{

super.onResume();
list.clear();
friendslist();
list_view.setAdapter(new ListActivityAdapter(this, list));

}

because list.clear();

friendslist() method list is null. but list.add call back.

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