How to open a database from not database class? <p>My database class is DB.java</p> <pre><code>package com.example.pocketbooker; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.provider.BaseColumns; public class DB extends SQLiteOpenHelper implements BaseColumns{ private static final String DATABASE_NAME = "pb_database.db"; private static final int DATABASE_VERSION = 1; public DB(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL("CREATE TABLE Members(_id INTEGER PRIMARY KEY AUTOINCREMENT, Mem_Name VARCHAR(50));"); db.execSQL("CREATE TABLE Incomes(_id INTEGER PRIMARY KEY AUTOINCREMENT, " + "Inc_Name VARCHAR(50), Inc_Sum VARCHAR(10), Inc_Mem VARCHAR (50), Inc_Change_Date DATE, " + "Inc_Perbeg_Date DATE, Inc_Period VARCHAR (4));"); db.execSQL("CREATE TABLE Outcomes(_id INTEGER PRIMARY KEY AUTOINCREMENT, " + "Out_Name VARCHAR(50), Out_Sum VARCHAR(10), Out_Mem VARCHAR (50), Out_Change_Date DATE, " + "Out_Perbeg_Date DATE, Out_Period VARCHAR (4));"); db.execSQL("CREATE TABLE Goals(_id INTEGER PRIMARY KEY AUTOINCREMENT, " + "Goal_Name VARCHAR(50), Goal_Sum VARCHAR(10), Goal_Mem VARCHAR (50), Goal_Change_Date DATE, " + "Goal_Perbeg_Date DATE, Goal_Period VARCHAR (4));"); } @Override public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) { // TODO Auto-generated method stub } } </code></pre> <p>My "other" class is Dialog_mem.java</p> <pre><code>package com.example.pocketbooker; import android.annotation.SuppressLint; import android.app.DialogFragment; import android.content.ContentValues; import android.content.Context; import android.content.DialogInterface; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.EditText; import android.widget.Toast; @SuppressLint({ "NewApi", "ValidFragment" }) public class Dialog_mem extends DialogFragment implements OnClickListener { EditText memname; ContentValues cv = new ContentValues(); private SQLiteDatabase database; private DB dbHelper; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { getDialog().setTitle("Добавить Члена Семьи"); View v = inflater.inflate(R.layout.mem_dialog, null); v.findViewById(R.id.mem_btnOK).setOnClickListener(this); v.findViewById(R.id.mem_btnCancel).setOnClickListener(this); memname=(EditText) v.findViewById(R.id.mem_name); return v; } public void onClick(View v) { switch(v.getId()) { case R.id.mem_btnOK: database = dbHelper.getWritableDatabase(); cv.put("Mem_Name", memname.getText().toString()); database.insert("Members", "Mem_Name", cv); database.close(); Toast.makeText(getActivity(), "Добавлено", Toast.LENGTH_SHORT).show(); default: dismiss();} } public void onDismiss(DialogInterface dialog) { super.onDismiss(dialog); } public void onCancel(DialogInterface dialog) { super.onCancel(dialog); } } </code></pre> <p>Eclipse shows <code>NullPointerException</code>. I guess it's context error, but I don't know how to point the context needed. <code>getActivity()</code> is the wrong one. "This" too. <code>getApplicationContext()</code> doesn't work at all.</p>