Ghanshyam
Ghanshyam

Reputation: 25

custom Adapter: why come this java.lang.NullPointerException?

Why I get java.lang.NullPointerException I try many way but still not working...

private class LoadAllQoutes extends AsyncTask<String, Void,  List<Quotescategory>> {

    @Override
    protected void onPreExecute() {

        super.onPreExecute();
        pDialog = new ProgressDialog(QuotesActivity2.this);
        pDialog.setMessage("Loading qoutes. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }


    protected List<Quotescategory> doInBackground(String... args) {

        List<NameValuePair> params = new ArrayList<NameValuePair>();

        JSONObject json = jParser.makeHttpRequest(url_all_qoutes, "GET", params);

        Log.d("All Qoutes: ", json.toString());

        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                QOUTES =json.getJSONArray(TAG_QOUTES);

                for (int i = 0; i < QOUTES.length(); i++) {

                    JSONObject c =QOUTES.getJSONObject(i);

                    quotescat.setId(c.getString(TAG_QID));
                    quotescat.setName(c.getString(TAG_NAME));
                    quotescat.set_By(c.getString(TAG_BY));
                    listcat.add(quotescat);

                    Log.d("All Qoutes: ", "Add on list");
                    Log.d("All Qoutes2: ", quotescat.getName().toString());

                }
            } else {

                Intent i = new Intent(getApplicationContext(),MainActivity.class);

                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                startActivity(i);
            }

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

        return null;
    }

    @SuppressWarnings("unused")
    protected void onPostExecute(List<Quotescategory> result) { 

       ListView Items = (ListView) findViewById(R.id.quoteslist);



        QuotesSimpleAdapter adapter=new QuotesSimpleAdapter(local, R.layout.list_item, result);
        Items.setAdapter(adapter);            

    }
}

 public class QuotesSimpleAdapter extends ArrayAdapter 
 {

    Context context;
    int layoutResourceId;
    List<Quotescategory> data = null;

    public QuotesSimpleAdapter(Context context,int layoutResourceId, List<Quotescategory> result)            {
        super(context,layoutResourceId, result);

        this.context = context;
        this.data = result;
        this.layoutResourceId = layoutResourceId;
    }


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

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

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

    @Override
   public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    RssItemHolder holder = null;

    if (row == null) {

        convertView = super.getView(position, convertView, parent);
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new RssItemHolder();

        holder.txt_id = (TextView) row.findViewById(R.id.txt_qid);

        holder.txt_by=(TextView) row.findViewById(R.id.txt_by);

        holder.txt_name=(TextView) row.findViewById(R.id.txt_name);

        row.setTag(holder);


    } else {

        holder = (RssItemHolder) row.getTag();
    }

    Quotescategory QItem = data.get(position);
    holder.txt_id.setText(QItem.getId());
    holder.txt_name.setText(QItem.getName());
    holder.txt_by.setText(QItem.get_By());

    return row;
}

static class RssItemHolder {

    TextView txt_id;
    TextView txt_name;
    TextView txt_by;
}

}

log cat:

11-08 11:30:48.558: E/AndroidRuntime(1147): FATAL EXCEPTION: main
11-08 11:30:48.558: E/AndroidRuntime(1147): java.lang.NullPointerException
11-08 11:30:48.558: E/AndroidRuntime(1147):     at com.example.guajratipride2.QuotesSimpleAdapter.getCount(QuotesSimpleAdapter.java:31)
11-08 11:30:48.558: E/AndroidRuntime(1147):     at android.widget.ListView.setAdapter(ListView.java:454)
11-08 11:30:48.558: E/AndroidRuntime(1147):     at com.example.pride.QuotesActivity2$LoadAllQoutes.onPostExecute(QuotesActivity.java:193)
11-08 11:30:48.558: E/AndroidRuntime(1147):     at com.example.pride.QuotesActivity2$LoadAllQoutes.onPostExecute(QuotesActivity.java:1)
11-08 11:30:48.558: E/AndroidRuntime(1147):     at android.os.AsyncTask.finish(AsyncTask.java:417)
11-08 11:30:48.558: E/AndroidRuntime(1147):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
11-08 11:30:48.558: E/AndroidRuntime(1147):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
11-08 11:30:48.558: E/AndroidRuntime(1147):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-08 11:30:48.558: E/AndroidRuntime(1147):     at android.os.Looper.loop(Looper.java:123)
11-08 11:30:48.558: E/AndroidRuntime(1147):     at android.app.ActivityThread.main(ActivityThread.java:3683)
11-08 11:30:48.558: E/AndroidRuntime(1147):     at java.lang.reflect.Method.invokeNative(Native Method)
11-08 11:30:48.558: E/AndroidRuntime(1147):     at java.lang.reflect.Method.invoke(Method.java:507)
11-08 11:30:48.558: E/AndroidRuntime(1147):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-08 11:30:48.558: E/AndroidRuntime(1147):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-08 11:30:48.558: E/AndroidRuntime(1147):     at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 256

Answers (4)

kiruwka
kiruwka

Reputation: 9450

NPE is caused by Adapter not having proper data. Add a sanity check in your getCount() :

@Override
public int getCount() { // list should be empty if data is null
    return data == null ? 0 : data.size();
}

Also, change your AsyncTask doInBackground to return your data for the list :

protected List<Quotescategory> doInBackground(String... args) {    
    //.... your code

    return result; // not null
}

Upvotes: 1

Abhishek Chaubey
Abhishek Chaubey

Reputation: 3010

return something(listcat); //instead of null int the doInbackground

Upvotes: 2

Blue_Alien
Blue_Alien

Reputation: 2177

You are returning "null " from doInBackground method:

please change this statement to :

 return result;

(i think you need to pass the listCat here.)

this result is passed to your

onPostExecute(List<Quotescategory> result)

Hope this is your problem

Upvotes: 1

Akash Moradiya
Akash Moradiya

Reputation: 3322

Change this code,

  @Override
   public View getView(int position, View convertView, ViewGroup parent) {

    RssItemHolder holder = null;

    if (convertView == null) {

        LayoutInflater inflater = LayoutInflater.from(context);
        convertView = inflater.inflate(layoutResourceId, null, false);
        holder = new RssItemHolder();

        holder.txt_id = (TextView) row.findViewById(R.id.txt_qid);

        holder.txt_by=(TextView) row.findViewById(R.id.txt_by);

        holder.txt_name=(TextView) row.findViewById(R.id.txt_name);

        convertView.setTag(holder);


    } else {

        holder = (RssItemHolder) convertView.getTag();
    }

    Quotescategory QItem = data.get(position);
    holder.txt_id.setText(QItem.getId());
    holder.txt_name.setText(QItem.getName());
    holder.txt_by.setText(QItem.get_By());

    return convertView;
}

i think this may help you

Upvotes: -1

Related Questions