Android NullPointerException 检索 SQlite 数据以通过 SimpleCursorAdapter 列出 [重复]

Posted

技术标签:

【中文标题】Android NullPointerException 检索 SQlite 数据以通过 SimpleCursorAdapter 列出 [重复]【英文标题】:Android NullPointerException on retrieving SQlite data to listiew thru SimpleCursorAdapter [duplicate] 【发布时间】:2017-02-27 06:51:14 【问题描述】:

我正在尝试通过简单的光标适配器通过新活动的意图将我的数据库填充到列表视图。不知道为什么会崩溃?

这是我的数据库助手类:

public class DBhelper extends SQLiteOpenHelper 

    public static final String DATABASE_NAME = "Mydatabase.db";
    public static final String TABLE_NAME = "contacts";
    public static final String _ID = "_ID";
    public static final String COL_2 = "NAME";
    public static final String COL_3 = "PHONE";
    public static final String COL_4 = "EMAIL";
    public static final String COL_5 = "HQ";
    public static final String COL_6 = "ADDRESS";


    public DBhelper(Context context) 
        super(context, DATABASE_NAME, null, 1);
    

    @Override
    public void onCreate(SQLiteDatabase db) 
        db.execSQL("create table contacts"+ "(_id integer primary key autoincrement, name text, phone text, email text, hq text, address text)");

    

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

    public boolean insertContact (String name, String phone, String email, String hq, String address)

        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_2,name);
        contentValues.put(COL_3,phone);
        contentValues.put(COL_4,email);
        contentValues.put(COL_5,hq);
        contentValues.put(COL_6,address);
        db.insert(TABLE_NAME,null,contentValues);
        return true;
    

    public Cursor getAllDAta()
        Cursor cursor;
        String[] columns = DBhelper._ID, DBhelper.COL_2, DBhelper.COL_3, DBhelper.COL_4, DBhelper.COL_5, DBhelper.COL_6;
        SQLiteDatabase db = this.getWritableDatabase();
        cursor = db.query(DBhelper.TABLE_NAME,columns, null, null, null, null, null);
        return cursor;
     

    DBhelper dbHelper;
    SQLiteDatabase database;
    Context context;
    public DBhelper open() throws SQLException 
    dbHelper = new DBhelper(context);
    database = dbHelper.getWritableDatabase();
    return this;

MainActivity.java

public class MainActivity extends AppCompatActivity 

    DBhelper mydb;
    EditText name, phone, email, hq, address;
    Button addData, viewData;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mydb = new DBhelper(this);
        name = (EditText) findViewById(R.id.editName);
        phone = (EditText) findViewById(R.id.editPhone);
        email = (EditText) findViewById(R.id.editEmail);
        hq = (EditText) findViewById(R.id.editHQ);
        address = (EditText) findViewById(R.id.editAddress);
        addData = (Button) findViewById(R.id.addData);
        viewData = (Button) findViewById(R.id.viewData);
    

    public void addClick(View view)

        String nm, ph, em, h, as;
        nm = name.getText().toString();
        ph = phone.getText().toString();
        em = email.getText().toString();
        h = hq.getText().toString();
        as = address.getText().toString();
        boolean checkk = mydb.insertContact(nm,ph,em,h,as);
        if (checkk = true) 
            Toast.makeText(MainActivity.this, "Data Added", Toast.LENGTH_LONG).show();
        else 
            Toast.makeText(MainActivity.this,"Data Not Added",Toast.LENGTH_LONG).show();

        
    

    public void showClick(View v1)
        Intent intent = new Intent(MainActivity.this,Displayc.class);
        startActivity(intent);
        finish();
    

我的展示活动

public class Displayc extends AppCompatActivity 
    ListView listView;
    DBhelper mydb1;
    SimpleCursorAdapter adapter;
    final String[] from = new String[] DBhelper._ID, DBhelper.COL_2, DBhelper.COL_3, DBhelper.COL_4, DBhelper.COL_5, DBhelper.COL_6;
    final int[] to = new int[] R.id.idv, R.id.namev, R.id.phonev, R.id.emailv, R.id.hqv, R.id.addressv ;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_displayc);
        listView = (ListView) findViewById(R.id.list_sq);
        mydb1 = new DBhelper(this.getBaseContext());
        mydb1.open();
        Cursor c = mydb1.getAllDAta();

        adapter = new SimpleCursorAdapter(getBaseContext(),R.layout.template,c,from,to,0);
        listView.setAdapter(adapter);
    

错误信息:

[java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.bibulkumar.sqliteapp/com.example.bibulkumar.sqliteapp.Displayc: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
                                                                             [Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference

【问题讨论】:

android 中的数据库区分大小写。在创建数据库以及使用小写字母插入和获取数据时,使用了小写字母作为列。请改正它可能会解决您的错误 【参考方案1】:
Context context;
public DBhelper open() throws SQLException 
    dbHelper = new DBhelper(context);
    database = dbHelper.getWritableDatabase();
    return this;

您在此处使用的context 未初始化,因此null。因此,当您调用getWritableDatabase() 时,NPE。

删除open() 以及对它的任何调用 - 实际上你不需要它来做任何事情。

【讨论】:

以上是关于Android NullPointerException 检索 SQlite 数据以通过 SimpleCursorAdapter 列出 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Android 中处理 NullPointerException?

<WebLogicServer; <BEA-000386; <Server subsystem failed. Reason: java.lang.NullPointerExcept

Maven打包时出现“Show Console View”错误弹出框,错误详情为“An internal error has occurred. java.lang.NullPointerExcept

Maven打包时出现“Show Console View”错误弹出框,错误详情为“An internal error has occurred. java.lang.NullPointerExcept

Android proguard 混淆代码实际上不应该导致 NullPointerException

RecyclerView的坑