Cursor c的NULL指针异常

时间:2013-04-15 16:27:46

标签: android database eclipse

public abstract class AbstractIME extends InputMethodService implements 
KeyboardView.OnKeyboardActionListener, CandidateView.CandidateViewListener {
    ...
    private void clearCandidates() {
        ...
        final dbhelper helper = new dbhelper(this);
        Cursor c = helper.query();
    }
    ...
}

public class CangjieDictionary extends WordDictionary {
    private AbstractIME ime;
    ....
    private String sortWords(char[] data) {
        ....
        dbhelper dbh = new dbhelper(ime); ;
        Cursor c = dbh.query();
    }
}

这是两个班级。对于第一个是好的,没问题 但第二类的代码相同

以下是logcat消息,为什么第二个类有空指针?

FATAL EXCEPTION: main
  java.lang.NullPointerException
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
at com.googlecode.tcime.unofficial.dbhelper.query(dbhelper.java:50)
at com.googlecode.tcime.unofficial.CangjieDictionary.sortWords(CangjieDictionary.java:111)

1 个答案:

答案 0 :(得分:0)

WordDictionary中声明一个Context成员并创建一个具有Context param

的构造函数
Context mContext;

public WordDictionary(Context context)
{
     mContext = context:
}  

然后在CangjieDictionary

public class CangjieDictionary extends WordDictionary {
private AbstractIME ime;
....
private String sortWords(char[] data) {
    ....
    dbhelper dbh = new dbhelper(mContext);
    Cursor c = dbh.query();
}

}