-2

HI I am new to android i am trying to retrieve the data from sqlite and getting nullpointer reference..Please help me on this.

Here is my code:-

Databasehelper.java

public class DatabaseHelper extends SQLiteOpenHelper{
    private final static String DATABASE_NAME="Reminder_details.db";
    private final static String TABLE_NAME="Reminder_detail_insert_tbl";
    private final static String CAL_NO1="Category_img";
    private final static String CAL_NO2="Category_Type";
    private final static String CAL_NO3="Title";
    private final static String CAL_NO4="Description";
    private final static String CAL_NO5="Repeat_type";
    private final static String CAL_NO6="Repeat_value";
    private final static String CAL_NO7="sun_Remind";
    private final static String CAL_NO8="mon_remind";
    private final static String CAL_NO9="tue_remind";
    private final static String CAL_NO10="wed_remind";
    private final static String CAL_NO11="thu_remind";
    private final static String CAL_NO12="fri_remind";
    private final static String CAL_NO13="sat_remind";
    private final static String CAL_NO14="active_date";
    private final static String CAL_NO15="active_end_date";
    private final static String CAL_NO16="creation_date";

    public DatabaseHelper(Context context) {

        super(context, DATABASE_NAME, null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL("Create Table " +TABLE_NAME +"(ID INTEGER PRIMARY KEY AUTOINCREMENT,Category_img BLOB,Category_Type TEXT,Title TEXT,Description TEXT,Repeat_type TEXT,Repeat_value INTEGER,sun_Remind TEXT,mon_remind TEXT,tue_remind TEXT,wed_remind TEXT,thu_remind TEXT,fri_remind TEXT,sat_remind TEXT,active_date TEXT,active_end_date TEXT,creation_date DATETIME DEFAULT CURRENT_TIMESTAMP)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
        onCreate(db);
    }


    public boolean DataInsert(
                    byte[] Category_img,
                    String Category_Type,
                    String Title,
                    String Description,
                    String Repeat_type,
                    int Repeat_value,
                    String sun_Remind,
                    String mon_Remind,
                    String tue_remind,
                    String wed_remind,
                    String thu_remind,
                    String fri_remind,
                    String sat_remind,
                    String active_date,
                    String active_end_date
        )
    {
        SQLiteDatabase db= getWritableDatabase();
        ContentValues cv=new ContentValues();
        cv.put(CAL_NO1,Category_img);
        cv.put(CAL_NO2,Category_Type);
        cv.put(CAL_NO3,Title);
        cv.put(CAL_NO4,Description);
        cv.put(CAL_NO5,Repeat_type);
        cv.put(CAL_NO6,Repeat_value);
        cv.put(CAL_NO7,sun_Remind);
        cv.put(CAL_NO8,mon_Remind);
        cv.put(CAL_NO9,tue_remind);
        cv.put(CAL_NO10,wed_remind);
        cv.put(CAL_NO11,thu_remind);
        cv.put(CAL_NO12,fri_remind);
        cv.put(CAL_NO13,sat_remind);
        cv.put(CAL_NO14,active_date);
        cv.put(CAL_NO15,active_end_date);


        long result=db.insert(TABLE_NAME,null,cv);

        if(result==-1)
            return false;
        else
            return true;


    }


    public Cursor getdata()
    {
        SQLiteDatabase db= getWritableDatabase();
        Cursor res=db.rawQuery("select * from "+TABLE_NAME,null);
        return res;
    }

}    

This is RecyclerAdaptor to get the data at run time from table please help me here.

in this recycleradaptor i am getting error while getting data from the table.

Cursor result=mydb.getdata(); in this line i am getting error.

RecyclerAdaptor.java

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>
{
    DatabaseHelper mydb;
    List<String> title_list,description_list;
    private String[] title ={"Timetable","Subjects","Faculty","Resources"};
    private String[] description={"description1","description2","description3","description4"};
    private int[] images={R.drawable.table,R.drawable.books,R.drawable.faculty,R.drawable.res};
//
//    public void getdatadb()
//    {
//
//
//        title_list=new ArrayList<String>();
//        description_list=new ArrayList<String>();
//
//
//        Cursor result=mydb.getdata();
//
//        if(result.getCount()==0)
//        {
//            return;
//        }else
//        {
//            while (result.moveToNext()){
//                title_list.add(result.getString(2));
//                description_list.add(result.getString(3));
//
//
//            }
//        }
//
//
//
//    }


    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView mTextTile,mTextDescription;
        ImageView mImageview;

        public ViewHolder(View itemView)
        {
            super(itemView);
            mTextTile=(TextView)itemView.findViewById(R.id.tvmain);
            mTextDescription=(TextView)itemView.findViewById(R.id.tvdescription);
            mImageview=(ImageView)itemView.findViewById(R.id.ivmain);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position=getAdapterPosition();
                    String text=mTextTile.getText().toString();
                    Snackbar.make(v,"Click Detected onItem "+position+" Item :"+text,Snackbar.LENGTH_LONG).setAction("Action",null).show();
                }
            });

        }
    }


    @Override
    public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_layout,parent,false);
        ViewHolder viewHolder=new ViewHolder(view);


        title_list=new ArrayList<String>();
        description_list=new ArrayList<String>();


        Cursor result=mydb.getdata();

        while (result.moveToNext()){
            title_list.add(result.getString(2));
            description_list.add(result.getString(3));
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerAdapter.ViewHolder holder, int position)
    {
        holder.mTextTile.setText(title_list.get(position));
        holder.mTextDescription.setText(description_list.get(position));
        holder.mImageview.setImageResource(images[position]);
    }

    @Override
    public int getItemCount() {
        return title.length;
    }

}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163

1 Answers1

0

Add this line at the start of "onCreateViewHolder()":

mydb = new DatabaseHelper(parent.getContext());

So your method is like:

@Override
public CustomAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
    mydb = new DatabaseHelper(parent.getContext());
    View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_layout,parent,false);
    ViewHolder viewHolder=new ViewHolder(view);


    title_list=new ArrayList<String>();
    description_list=new ArrayList<String>();


    Cursor result=mydb.getdata();

    while (result.moveToNext()){
        title_list.add(result.getString(2));
        description_list.add(result.getString(3));
    }
    return viewHolder;
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Ankit Patidar
  • 2,731
  • 1
  • 14
  • 22