-1

I'm trying to populate my database to listview through simple cursor adapter via intent to new activity. Cant figure out why is it crashing?

This is my Database helper class:

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();
    }
}

My display activity

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);
    }
}

Error message:

[java.lang.RuntimeException: Unable to start activity ComponentInfo{com.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
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 1
    Database in android is case sensitive. Use have used small letters for columns while creating database and inserting and fetching data using small letters. Please make that correct it may resolve your error – Aditi Oct 18 '16 at 06:15

1 Answers1

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

The context you use here is not initialized and therefore null. Hence the NPE when you're calling getWritableDatabase().

Remove the open() and any calls to it - you don't actually need it for anything.

laalto
  • 150,114
  • 66
  • 286
  • 303