vendredi 31 juillet 2015

Getting java.lang.NullPointerException in getView of Adapter class while running the App

I am trying to display rows from my table in SQLite database in custom ListView for that i created an ProductAdapter class . I am getting an error while running the App Following is the error

07-31 14:31:42.548    1968-1968/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.sling.freshecomart.ProductAdapter.getView(ProductAdapter.java:54)
            at android.widget.AbsListView.obtainView(AbsListView.java:2267)
            at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
            at android.widget.ListView.onMeasure(ListView.java:1156)
            at android.view.View.measure(View.java:15172)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
            at android.view.View.measure(View.java:15172)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
            at android.view.View.measure(View.java:15172)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
            at android.view.View.measure(View.java:15172)
            at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
            at android.view.View.measure(View.java:15172)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
            at android.view.View.measure(View.java:15172)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
            at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:444)
            at android.view.View.measure(View.java:15172)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:15172)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
            at android.view.View.measure(View.java:15172)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
            at android.view.View.measure(View.java:15172)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
            at android.view.Choreographer.doCallbacks(Choreographer.java:555)
            at android.view.Choreographer.doFrame(Choreographer.java:525)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

following is the line ProductAdapter.java:54

convertView = mInflater.inflate(R.layout.item,null);

ProductAdapter class is

public class ProductAdapter extends BaseAdapter {
    private List<com.sling.freshecomart.Product> mProductList;
    private LayoutInflater mInflater;
    private Activity activity;
    private LayoutInflater inflater=null;
    public ProductAdapter(List<Product> list, Activity a) {
        this.mProductList = list;
        this.activity=a;


        inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    @Override
    public int getCount() {
        return mProductList.size();
    }

    @Override
    public Object getItem(int position) {
        return mProductList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewItem item;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item,null);
            item = new ViewItem();

            item.post_id=(TextView)convertView.findViewById(R.id.post_id);
            item.Name = (TextView)convertView.findViewById(R.id.Name);
            item.Weight = (TextView)convertView.findViewById(R.id.Weight);
            item.Description = (TextView)convertView.findViewById(R.id.Description);
            item.saveValue = (TextView)convertView.findViewById(R.id.saveValue);
            item.mrpValue = (TextView)convertView.findViewById(R.id.mrpValue);
            item.Price = (TextView)convertView.findViewById(R.id.Price);

            convertView.setTag(item);
        } else {
            item = (ViewItem) convertView.getTag();
        }

        com.sling.freshecomart.Product cartProduct = mProductList.get(position);

        item.Name.setText(cartProduct.Name);
        item.Weight.setText(cartProduct.Weight);
        item.Description.setText(cartProduct.Description);
        item.mrpValue.setText(cartProduct.mrpValue);
        item.saveValue.setText(cartProduct.saveValue);
        item.Price.setText(cartProduct.Price);
        return convertView;
    }

    private class ViewItem {
        private TextView post_id,Name,Weight,Description,mrpValue,saveValue,Price;
 }
}

Product class is

public class Product {
    public String post_id,Name,Weight,Description;
    public int mrpValue,saveValue,Price;
    public Drawable productImage;
    public boolean selected;

    public Product(String Name,String Wieght,String Description,int mrpValue,int saveValue,int Price) {
        this.Name = Name;
        this.Weight=Wieght;
        this.Description = Description;
        this.mrpValue=mrpValue;
        this.saveValue=saveValue;
        this.Price = Price;
    }
}

oncreate() of The activity where the list should be displayed

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);

        cartDB= new SQLiteHandler(this);
        Cursor res= cartDB.getCartDetails();

        ListView lv_cart = (ListView) findViewById(R.id.ListViewCart);

        List<Product> cartList = new ArrayList<>();
        int NoOfItemsInCart = res.getCount();

        if(res.getCount()==0){
            showMessage("Error","No rows Found");
        }else {
            res.moveToFirst();
            while (NoOfItemsInCart != 0) {

                res.getString(0);
                String CName = res.getString(1);
                String CWeight = res.getString(2);
                String CDescription = res.getString(3);
                int CmrpValue = res.getInt(4);
                int CsaveValue = res.getInt(5);
                int CPrice = res.getInt(6);
                cartList.add(new Product(CName, CWeight, CDescription, CmrpValue, CsaveValue, CPrice));
                res.moveToNext();
                NoOfItemsInCart = NoOfItemsInCart - 1;
            }
            ProductAdapter adapter = new ProductAdapter(cartList, this);
            lv_cart.setAdapter(adapter);
        }

Please help and Thanks in Advance

Aucun commentaire:

Enregistrer un commentaire