尝试在sqlite中的空对象引用上调用虚拟方法[重复]
Posted
技术标签:
【中文标题】尝试在sqlite中的空对象引用上调用虚拟方法[重复]【英文标题】:Attempt to invoke virtual method on a null object reference in sqlite [duplicate] 【发布时间】:2018-05-19 21:34:14 【问题描述】:您好,我是 android 新手,我正在尝试从 sqlite 检索数据并获取 nullpointer
参考。请帮助我。
这是我的代码:-
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;
这是 RecyclerAdaptor 在运行时从表中获取数据,请在此处帮助我。
在这个 recycleradaptor 中,我在从表中获取数据时出错。
Cursor result=mydb.getdata();
在这一行中我遇到了错误。
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;
【问题讨论】:
在这里发布您的错误 mydb 未初始化 【参考方案1】:在"onCreateViewHolder()"
的开头添加这一行:
mydb = new DatabaseHelper(parent.getContext());
所以你的方法是这样的:
@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;
【讨论】:
非常感谢,这个解决了我的问题......我知道为什么我们需要添加这一行,你能解释一下吗......所以将来我可能会遇到这种问题。 访问数据库方法需要初始化“Database Helper”。 谢谢,如果您需要更多东西,请告诉我。以上是关于尝试在sqlite中的空对象引用上调用虚拟方法[重复]的主要内容,如果未能解决你的问题,请参考以下文章
尝试在 recyclerview kotlin 上的空对象引用上调用虚拟方法 'void android.widget.TextView.setText(java.lang.CharSequence)
显示来自sqlite数据库的数据时“尝试在空对象引用上调用虚拟方法”[重复]
java.lang.NullPointerException:尝试在 OnPostExecute() 上的空对象引用错误上调用接口方法 - AsyncTask
尝试在空对象引用上调用虚拟方法“java.lang.String android.content.Context.getPackageName()”