prof_jack
prof_jack

Reputation: 2073

null pointer exception in android?

enter image description here I want my application to look like this. I am able to get names and types in two different arrays but getting null pointer exception at line marked in the code.names and type array are getting values as I intended.I have double checked my layout files one containing list view and other containing two text view.any help would be greatly appreciable..

 package application.test;
    import java.util.HashMap;
    import android.app.ListActivity;
    import android.content.ContentResolver;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.provider.ContactsContract;
    import android.widget.ListView;

        public class TestActivity<types, names> extends ListActivity{

            int count[];
            int typecount[];
            ListView lv;
            ListViewAdapterrecent lva;
            String[] names;
            String[] types;
            /** Called when the activity is first created. */
            @Override
            public void onCreate(Bundle savedInstanceState) {  
                super.onCreate(savedInstanceState);

                setContentView(R.layout.main);
                lv=(ListView)findViewById(android.R.id.list);

                ContentResolver tcr = getContentResolver();
                 Cursor tcur=tcr.query(ContactsContract.Data.CONTENT_URI, null, null, null, null);


                 HashMap<Integer, String> typehashmap=new HashMap<Integer, String>();
               HashMap<Integer, String> namehashmap=new HashMap<Integer, String>();

               if(tcur.getCount()>0)
               { 
                  while(tcur.moveToNext())
                  {
                  Boolean temp=false;   
                  String nvalues=tcur.getString(tcur.getColumnIndex(ContactsContract.Data.DATA2));

                  if (Integer.parseInt(nvalues)==1){
                   String value="home";
                   temp=true;
                  String rw=tcur.getString(tcur.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));
                  int key=Integer.parseInt(rw);
                  typehashmap.put(key, value);
                  }

                  else if(Integer.parseInt(nvalues)==2)
                  {
                   String value="mobile";
                  temp=true;
                   String rw=tcur.getString(tcur.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));
                   int key=Integer.parseInt(rw);
                   typehashmap.put(key, value);
                  }

                  else
                  {
                  String value="work";
                  temp=true;
                   String rw=tcur.getString(tcur.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));
                   int key=Integer.parseInt(rw);
                   typehashmap.put(key, value);
                  }

                  if(temp==true)
                  {
                      tcur.moveToNext();
                      String rw=tcur.getString(tcur.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID)); 
                      int key=Integer.parseInt(rw);
                      String zvalues=tcur.getString(tcur.getColumnIndex(ContactsContract.Data.DATA2));
                       namehashmap.put(key, zvalues);
                     }
                 }//while
               tcur.close();

        types=  typehashmap.values().toArray(new String[typehashmap.size()]);
          names= namehashmap.values().toArray(new String[namehashmap.size()]);
               lva=new ListViewAdapterrecent(this,names,types);            
           lv.setAdapter(lva);
 }
         }
       }

    listviewrecent.java.............

    package application.test;
    import android.app.Activity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.TextView;

    public class ListViewAdapterrecent extends BaseAdapter{

        Activity context;
        String[] names;
        String[] types;

        public ListViewAdapterrecent(Activity context, String[] names, String[] types) {
            // TODO Auto-generated constructor stub

        this.context=context;
        this.names=names;
        this.types=types;
        }   

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return names.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        public class viewHolder {
            TextView top;
            TextView bottom;
        }

          public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            viewHolder holder;
            if(convertView==null){

           LayoutInflater inflator=context.getLayoutInflater();
            convertView=inflator.inflate(R.layout.textviewonly,null);

            holder=new viewHolder();
            holder.top=(TextView)convertView.findViewById(R.id.toptext);
            holder.bottom=(TextView)convertView.findViewById(R.id.bottomtext);
            convertView.setTag(holder);
            }else{
                holder=(viewHolder)convertView.getTag();
            }

            holder.top.setText(names[position]);
            holder.bottom.setText(types[position]);

            return convertView;
        }
    }

Upvotes: 0

Views: 945

Answers (4)

prof_jack
prof_jack

Reputation: 2073

The edited version is just working fine.I was passing parameters names and types to class listviewadapterrecent before they were actually created thus passing null to the adapter class and getting null values to list view thus getting null pointer exception.

Upvotes: 0

Iulia Barbu
Iulia Barbu

Reputation: 1530

means that in your main layout , you don't have a listview with id "list"

Upvotes: 0

Pratik
Pratik

Reputation: 30855

just remove the comment from this line

 setContentView(R.layout.main);

reason why

because you getting list object from the xml file using its id so you have to first set that layout which contain that control.

updated:

you merge to concept here You extends the ListActivity and also get the ListView object from the xml file using it's id. Either you can extends the Activity with this code and run it or you can put this lines into the comment and run it.

Lines are:

lv=(ListView)findViewById(android.R.id.list);

and write this way

lv = getListView();

If you extends the ListActivity then you can get the current ListView object like this way or if you want to customize then extends Activity instead of ListActivity

check this http://www.vogella.de/articles/AndroidListView/article.html aritcles

Upvotes: 5

student
student

Reputation: 213

Make sure the variable for lv is not a null. You can try to test if

    lv=(ListView)findViewById(android.R.id.list);

is working by ensuring your lv doesn't receive a null.

Upvotes: 0

Related Questions