Search code examples
androidoopandroid-togglebutton

model object is null


i'm trying to save list items by toggle button click. when i click on toggle button my app crashes, i debugged my app and found that the model object every time is null !! can anyone tell me why it is null ? how can i solve this? i tried to handle toggle button click out of the adapter class but in vain

here is myAdapter.java

package com.example.remon.my_app2;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.example.remon.my_app2.models.CatModel;

import java.util.ArrayList;

/**
 * Created by Remon on 9/27/2017.
 */

public class myAdapter extends BaseAdapter{

    ArrayList<CatModel> catModels;
    Context mContext;


    ToggleButton btnTglFavourite;
    DatabaseHelper databaseHelper;
    CatModel catModel;
    public myAdapter(Context context,ArrayList<CatModel>catModels){
        this.mContext=context;
        this.catModels=catModels;

    }
    @Override
    public int getCount() {
        return catModels == null ? 0 : catModels.size();
    }

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

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

    @Override
    public View getView(int position, View ConvertedView, ViewGroup viewGroup) {
        View view=ConvertedView;
        if (view==null)
        {
            LayoutInflater inflater=(LayoutInflater)
                    mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view=inflater.inflate(R.layout.recycler_1_items,viewGroup,false);

            CatModel catModel = (CatModel)getItem(position);
            TextView txtCatName = (TextView)view.findViewById(R.id.txtCat);
             txtCatName.setText(catModel.getCatName());
        }
        btnTglFavourite = (ToggleButton)view.findViewById(R.id.favoriteToggleButton);
//            btnTglFavourite.setChecked(false);
//            if (databaseHelper.check(catModelss.getCatName())){
//                btnTglFavourite.setChecked(true);
//            }
        btnTglFavourite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!databaseHelper.check(catModel.getCatName())){  //////object is null !!
                    databaseHelper.insert(catModel.getCatName());
                    Toast.makeText(mContext,"in database: " +catModel.getCatName(),Toast.LENGTH_SHORT).show();
                }
                else {databaseHelper.deleteRow(catModel.getCatName());}
            }
        });
        return view;
    }
}

Solution

  • The catmodel you are using below

        if (!databaseHelper.check(catModel.getCatName())){
                    databaseHelper.insert(catModel.getCatName());
                    Toast.makeText(mContext,"in database: "+catModel.getCatName(),Toast.LENGTH_SHORT).show();
        }
        else {databaseHelper.deleteRow(catModel.getCatName());}
    

    is the global/field reference which never got initialized.

    Solution should be

        public View getView(int position, View ConvertedView, ViewGroup viewGroup) {
            final CatModel catModel = (CatModel)getItem(position);
            View view=ConvertedView;
            if (view==null){
                LayoutInflater inflater=(LayoutInflater)
                        mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view=inflater.inflate(R.layout.recycler_1_items,viewGroup,false);
    
                TextView txtCatName = (TextView)view.findViewById(R.id.txtCat);
                 txtCatName.setText(catModel.getCatName());
            }
            btnTglFavourite = (ToggleButton)view.findViewById(R.id.favoriteToggleButton);
    //            btnTglFavourite.setChecked(false);
    //            if (databaseHelper.check(catModelss.getCatName())){
    //                btnTglFavourite.setChecked(true);
    //            }
            btnTglFavourite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (!databaseHelper.check(catModel.getCatName())){  //////object is null !!
                        databaseHelper.insert(catModel.getCatName());
                        Toast.makeText(mContext,"in database: " +catModel.getCatName(),Toast.LENGTH_SHORT).show();
                    }
                    else {databaseHelper.deleteRow(catModel.getCatName());}
                }
            });
            return view;
        }