NullPointerException when calling getReadableDatabase()

故事的背景,甲午年末,某大型项目突遇新需求,数据库需由Mysql转到SQLite,于是百万大军浩浩汤汤的代码搬迁开始了,途中几经突围,终于杀出一片天,前方传来捷报,哈哈~~

这个问题困扰了好几天,在stackoverflow上搜了很多帖子,都没能解决,比如:

getReadableDatabase() Null Pointer Exception

NullPointerException on getReadableDatabase()

SQLite Android cannot open database file

等。

最后还是参考一个例子,加上我这几天在sqlite的编码经验,终于解决了这个问题。

1.首先是DbHelper类(extends SQLiteOpenHelper)的构造函数:

public DbHelper(Context context) {

super(context, DATABASE_NAME, null, DATABASE_VERSION);

// TODO Auto-generated constructor stub

}

onCreate()等三个方法就不细说了。

2.写一个sqlite的操作类

private DbHelper dbHelper;

private SQLiteDatabase db;

//构造函数,很关键,就是new了一个DbHelper对象,初始化SQLiteDatabase

//在Android开发中Context很重要,往往是普通类和Activity等Android平台类的桥梁。

public DbOperations(Context context){

dbHelper = new DbHelper(context);

}

//封装sqlite的打开方法

public void open() throws SQLException{

db = dbHelper.getReadableDatabase();

}

//封装sqlite的关闭方法0

public void close(){

dbHelper.close();

}

举例使用:

//获取用户的观看记录

public List getViewRecord(User user){

List list = new ArrayList();

String sql = "select * from showlist where showId in(select  showId from  viewrecord where userId=?)";

// myDbHelper = new DbHelper(LoginActivity.this,DbHelper.DATABASE_NAME,null,DbHelper.DATABASE_VERSION);

// db = myDbHelper.getReadableDatabase();

this.open();

Cursor cursor = db.rawQuery(sql, new String[]{String.valueOf(user.getUserId())});

while(cursor.moveToNext()){

//String showName = rs.getString("showName");

list.add(new Show(cursor.getInt(0),cursor.getString(1),cursor.getString(2)) );

Log.i("sql-getViewRecord",cursor.getInt(0) + cursor.getString(1) + cursor.getString(2));

// System.out.println("视频名:" + showName);

}

cursor.close();

this.close();

return list;

}

然后就可以在需要的地方调用此方法了。

more information refer to :

Android SQLite Example(recommendation):

http://examples.javacodegeeks.com/android/core/database/sqlite/sqlitedatabase/android-sqlite-example/

Android SQLite Database Tutorial:

http://www.tutorialspoint.com/android/android_sqlite_database.htm

时间: 2024-07-31 16:51:30

NullPointerException when calling getReadableDatabase()的相关文章

架构设计:系统间通信(7)——IO通信模型和Netty 下篇

接上文<架构设计:系统间通信(6)--IO通信模型和Netty 上篇> 5.再次审视为什么使用Netty 上篇文章我们讨论了Netty的基本原理,重要概念,并使用java代码描述了Netty的基本使用.当然Netty的技术涵盖点远远不是那一篇基础代码就可以全部概括的,但是至少可以给读者一个切入点.让大家去思考一个我们一直在讨论的问题:为什么有了JAVA NIO框架后我们还需要有Netty这样的框架对底层再次进行封装? 5-1.IO模型的封装 5-1-1.再次总结IO模型 在前文中我们已经提到了

[Android基础系列]重新审视ContentProvider

前言 上一篇文章梳理了一下Service,这一篇文章梳理一下同为四大组件之一的ContentProvider,也试着使用在线MarkDown编辑器.正常开发中,我们使用ContentProvider较少,大家也不会太过于关注.国内能看到的几篇文章也是比较老旧了,demo中也有一些错误,所以写一篇"像样"的,凑凑数. 正文 概述ContentProvider 依旧去API文档中先拿到官方的概述,并做一点蹩脚的翻译和注解: Content providers are one of the

findViewById中NullPointerException的错误

最近在弄一个对话框的登录时,发现一个总是报NullPointerException的错误,折腾了两小时,一直没有发现细小的区别..先上图,一边说明原因 首先是 Activity类中定义的findViewById() * There are two methods almost all subclasses of Activity will implement: * * <ul> * <li> {@link #onCreate} is where you initialize you

Java Tips and Best practices to avoid NullPointerException

A NullPointerException in Java application is best way to solve it and that is also key to write robust programs which can work smoothly. As it said “prevention is better than cure”, same is true with nasty NullPointerException. Thankfully by applyin

项目启动时出现Exception in thread &quot;HouseKeeper&quot; java.lang.NullPointerException

解决方案: 首先建立一个servlet类: package cn.ydc.framework.util; import java.io.IOException; import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; i

在Servlet使用getServletContext()获取ServletContext对象出现java.lang.NullPointerException(空指针)异常的解决办法

今天遇到了一个在servlet的service方法中获取ServletContext对象出现java.lang.NullPointerException(空指针)异常,代码如下: 1 //获取ServletContext对象 2 ServletContext servletContext = this.getServletContext(); 这个问题很奇怪,也是第一次遇到,因为以前在servlet的doGet/doPost方法中要获取ServletContext对象时都是这样写的,也没有出现过

解决kylin build cube第一步报错:java.lang.NullPointerException

报错栈: 2017-06-19 10:27:35,757 ERROR [pool-9-thread-4] threadpool.DefaultScheduler:140 : ExecuteException job:933bc47a-302c-48fa-8ec9-ae8730057175 org.apache.kylin.job.exception.ExecuteException: org.apache.kylin.job.exception.ExecuteException: java.la

再见NullPointerException。在Kotlin里null的处理(KAD 19)

作者:Antonio Leiva 时间:Apr 4, 2017 原文链接:https://antonioleiva.com/nullity-kotlin/ 关于Kotlin最重要的部分之一:无效处理,我花了很长时间完成写一篇文章. 东尼·霍尔(Tony Hoare)"空(null)"概念的创作者,自称"十亿美元的错误".在你编写Java代码是,null是最容易出错的指针. 如果你在观察Bug管理器,我确信你见到的NullPointerException错误要超过90

Python的最大递归深度错误 “maximum recursion depth exceeded while calling a Python object”

今天在写爬虫的时候,发现了一个诡异的事情,使用str方法强制转换一个BeautifulSoup对象成字符串的时候报错了,提示是"maximum recursion depth exceeded while calling a Python object",意思大致是"当调用该对象超过最大递归深度" 报错如下:   Traceback (most recent call last):   File "<stdin>", line 1,