Я пытаюсь сохранить данные в базе данных Sqlite. Я создал вспомогательный класс, который расширяет Sqlite openhelper class.
Я написал весь код для создания таблицы базы данных и столбцов.
Я вызываю метод savedata
, когда нажимаю на кнопку сохранения в моей MainActivity .
Проблема возникла, когда я написал код для базы данных getwriteable.
Приложение не ответило и закрылось.
public class ClientDetailsActivity extends AppCompatActivity {
EditText client_name, client_address, client_mobile_number, client_email_id, policy_name, policy_number;
Button save, edit, delete;
ListView listView;
SQLiteDatabase sqLiteDatabase;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client_details);
client_name = findViewById(R.id.client_name);
client_address = findViewById(R.id.client_address);
client_mobile_number = findViewById(R.id.client_mobile_number);
client_email_id = findViewById(R.id.client_email_id);
policy_name = findViewById(R.id.et_policy_name);
policy_number = findViewById(R.id.et_policy_number);
save = findViewById(R.id.btn_save);
edit = findViewById(R.id.btn_edit);
delete = findViewById(R.id.btn_delete);
listView = findViewById(R.id.list);
}
public void Savedata(View view) {
Helper helper=new Helper(context);
helper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(Helper.COL_CLIENT_NAME,client_name.getText().toString());
values.put(Helper.COL_CLIENT_ADDRESS,client_address.getText().toString());
values.put(Helper.COL_CLIENT_MOBILE_NUMBER,client_mobile_number.getText().toString());
values.put(Helper.COL_CLIENT_EMAIL_ID,client_mobile_number.getText().toString());
values.put(Helper.COL_POLICY_NAME,policy_name.getText().toString());
values.put(Helper.COL_POLICY_NUMBER,policy_number.getText().toString());
String clientname=client_name.getText().toString();
if(clientname.equals("")) {
long id = sqLiteDatabase.insert(Helper.CLIENT_TABLE,null,values);
Toast.makeText(context,"ID"+id,Toast.LENGTH_LONG).show();
}
}
}
Error
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
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.example.smartagent.ClientDetailsActivity$override.Savedata(ClientDetailsActivity.java:46)
at com.example.smartagent.ClientDetailsActivity$override.access$dispatch(ClientDetailsActivity.java)
at com.example.smartagent.ClientDetailsActivity.Savedata(ClientDetailsActivity.java)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I/Process: Sending signal. PID: 10482 SIG: 9
Application terminated.