user5425093 user5425093 - 28 days ago 28
Java Question

Android Studio: Failed to execute onClick

I am getting the following error. I am unable to save the values in the database, as my app crashes as soon as I click the "Add Patient" button.

I HAVE PROVIDED THE LOG CAT BELOW, I HOPE THAT WILL HELP YOU UNDERSTAND THE ERROR I AM GETTING.

ALSO, AS THIS IS BIG PROJECT, I HAVE NOT INCLUDED ALL MY FILES, SO PLEASE FEEL FREE TO ASK ME FOR ANYTHING ADDITIONAL, AND I WILL POST.

Here is my code:

This gole is to simply grab the values from the EditText and Drowdown section, and insert them into the DataBase.



AddPatient.java

package tanav.sharma;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class AddPatient extends AppCompatActivity {

EditText PatientFirstName, PatientLastName;
Spinner departments;
Context context;
PatientDbHelper patientDbHelper;

SQLiteDatabase sqLiteDatabase;

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

getSupportActionBar().setTitle(R.string.add_patient);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);


PatientFirstName = (EditText) findViewById(R.id.etFirsNameEnter);
PatientLastName = (EditText) findViewById(R.id.etLastNameEnter);
departments = (Spinner) findViewById(R.id.departmentSelector);

String[] items = new String[]{"Audiology", "Sociology", "Minoscopiology", "Simpliology", "Taniology"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, items);
departments.setAdapter(adapter);
}

public void addPatient(View view){

String fname = PatientFirstName.getText().toString();
String lname = PatientLastName.getText().toString();
String dept = departments.getSelectedItem().toString();

patientDbHelper = new PatientDbHelper(context);
sqLiteDatabase = patientDbHelper.getWritableDatabase();
patientDbHelper.addInformations(fname,lname,dept,sqLiteDatabase);
Toast.makeText(getBaseContext(),"Data Saved",Toast.LENGTH_LONG).show();
patientDbHelper.close();

}
}





Here is the code to how I created the Database..

PatientDbHelper.java

package tanav.sharma;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
* Created by Tanav on 11/4/2016.
*/
public class PatientDbHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "PATIENTINFO.db";
private static final int DATABASE_VERSION = 1;

private static final String CREATE_QUERY =
"CREATE TABLE "+PatientInfo.NewPatientInfo.TABLE_NAME+" ("+ PatientInfo.NewPatientInfo.PATIENT_FNAME+ " TEXT,"+PatientInfo.NewPatientInfo.PATIENT_LNAME+" TEXT,"+PatientInfo.NewPatientInfo.PATIENT_DEPARTMENT+" TEXT);";

public PatientDbHelper(Context context){

super(context,DATABASE_NAME,null,DATABASE_VERSION);
Log.e("DATABASE OPERATIONS","Database created / opened ...");
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_QUERY);
Log.e("DATABASE OPERATIONS","Table created...");

}

public void addInformations(String fname, String lname, String department, SQLiteDatabase db){

ContentValues contentValues = new ContentValues();
contentValues.put(PatientInfo.NewPatientInfo.PATIENT_FNAME,fname);
contentValues.put(PatientInfo.NewPatientInfo.PATIENT_LNAME,lname);
contentValues.put(PatientInfo.NewPatientInfo.PATIENT_DEPARTMENT,department);

db.insert(PatientInfo.NewPatientInfo.TABLE_NAME, null,contentValues);

Log.e("DATABASE OPERATIONS","One in row inserted...");




}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
}





Code for Patient info;

PatientInfo.java

package tanav.sharma;

/**
* Created by Tanav on 11/4/2016.
*/
public class PatientInfo {

public static abstract class NewPatientInfo{

public static final String PATIENT_FNAME = "patient_fname";
public static final String PATIENT_LNAME = "patient_lname";
public static final String PATIENT_DEPARTMENT = "patient_department";

public static final String TABLE_NAME = "patient_info";
}
}


LOGCAT:

11-04 15:03:54.453 17167-17167/tanav.sharma D/ViewRootImpl: ViewPostImeInputStage processPointer 0
11-04 15:03:54.503 17167-17167/tanav.sharma D/ViewRootImpl: ViewPostImeInputStage processPointer 1
11-04 15:03:57.173 17167-17167/tanav.sharma D/ViewRootImpl: ViewPostImeInputStage processPointer 0
11-04 15:03:57.273 17167-17167/tanav.sharma D/ViewRootImpl: ViewPostImeInputStage processPointer 1
11-04 15:03:57.273 17167-17167/tanav.sharma I/Timeline: Timeline: Activity_launch_request id:tanav.sharma time:114524439
11-04 15:03:57.373 17167-17167/tanav.sharma D/SecWifiDisplayUtil: Metadata value : none
11-04 15:03:57.373 17167-17167/tanav.sharma D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{f3b43b7 I.E...... R.....ID 0,0-0,0}
11-04 15:03:57.453 17167-17167/tanav.sharma D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 48 - 0, 0) vi=Rect(0, 48 - 0, 0) or=1
11-04 15:03:57.483 17167-17219/tanav.sharma D/OpenGLRenderer: endAllStagingAnimators on 0xb776ac00 (RippleDrawable) with handle 0xb7715830
11-04 15:03:57.483 17167-17167/tanav.sharma I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@7415ab6 time:114524649
11-04 15:03:57.733 17167-17167/tanav.sharma V/ActivityThread: updateVisibility : ActivityRecord{65e1ce7 token=android.os.BinderProxy@ef446b8 {tanav.sharma/tanav.sharma.TanavActivity}} show : false
11-04 15:03:59.843 17167-17167/tanav.sharma D/ViewRootImpl: ViewPostImeInputStage processPointer 0
11-04 15:03:59.953 17167-17167/tanav.sharma D/ViewRootImpl: ViewPostImeInputStage processPointer 1
11-04 15:04:00.083 17167-17167/tanav.sharma D/ViewRootImpl: MSG_RESIZED: ci=Rect(0, 48 - 0, 0) vi=Rect(0, 48 - 0, 590) or=1
11-04 15:04:01.183 17167-17167/tanav.sharma D/ViewRootImpl: ViewPostImeInputStage processKey 0
11-04 15:04:01.253 17167-17167/tanav.sharma D/ViewRootImpl: ViewPostImeInputStage processKey 1
11-04 15:04:05.453 17167-17167/tanav.sharma D/ViewRootImpl: ViewPostImeInputStage processKey 0
11-04 15:04:05.453 17167-17167/tanav.sharma D/ViewRootImpl: ViewPostImeInputStage processKey 1
11-04 15:04:05.463 17167-17167/tanav.sharma W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection
11-04 15:04:12.853 17167-17167/tanav.sharma D/ViewRootImpl: ViewPostImeInputStage processKey 0
11-04 15:04:12.853 17167-17167/tanav.sharma D/ViewRootImpl: ViewPostImeInputStage processKey 1
11-04 15:04:12.893 17167-17167/tanav.sharma D/ViewRootImpl: MSG_RESIZED: ci=Rect(0, 48 - 0, 0) vi=Rect(0, 48 - 0, 0) or=1
11-04 15:04:14.593 17167-17167/tanav.sharma D/ViewRootImpl: ViewPostImeInputStage processPointer 0
11-04 15:04:14.743 17167-17167/tanav.sharma D/ViewRootImpl: ViewPostImeInputStage processPointer 1
11-04 15:04:14.743 17167-17167/tanav.sharma E/DATABASE OPERATIONS: Database created / opened ...
11-04 15:04:14.743 17167-17167/tanav.sharma D/AndroidRuntime: Shutting down VM
11-04 15:04:14.753 17167-17167/tanav.sharma E/AndroidRuntime: FATAL EXCEPTION: main
Process: tanav.sharma, PID: 17167
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5716)
at android.widget.TextView.performClick(TextView.java:10926)
at android.view.View$PerformClick.run(View.java:22596)
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:7331)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5716) 
at android.widget.TextView.performClick(TextView.java:10926) 
at android.view.View$PerformClick.run(View.java:22596) 
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:7331) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
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 tanav.sharma.AddPatient.addPatient(AddPatient.java:49)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:5716) 
at android.widget.TextView.performClick(TextView.java:10926) 
at android.view.View$PerformClick.run(View.java:22596) 
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:7331) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)


 

Answer

context is null, change the line:

  patientDbHelper = new PatientDbHelper(context);

for

  patientDbHelper = new PatientDbHelper(this);
Comments