When I try to add data to database by clicking on a button in android studio my program crashes.
package com.example.ganesha.myapplication;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText StudentName,StudentRollno;
// String selectedmess;
Context context = this;
StudentDbHelper studentDbHelper;
SQLiteDatabase sqLiteDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter;
adapter = ArrayAdapter.createFromResource (this,R.array.mess,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedmess=spinner.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
public void addDetail(View view)
{
String name = StudentName.getText().toString();
String rollno = StudentRollno.getText().toString();
studentDbHelper = new StudentDbHelper(context);
sqLiteDatabase = studentDbHelper.getWritableDatabase();
studentDbHelper.addInfo(name,rollno,sqLiteDatabase);
Toast.makeText(getBaseContext(),"One row data saved",Toast.LENGTH_LONG).show();
studentDbHelper.close();
}
}
The above is mainactivity.java file
Databasefirst.java file
package com.example.ganesha.myapplication; public class Databasefirst { public static abstract class studentinfo{ public static final String STUDENT_NAME="Student Name";//column name public static final String STUDENT_ROLLNO="Student Roll no";//column name // public static final String MESS_NAME="Mess Alloted";//column name public static final String MESS_DETAILS="Mess Details";//table name } }
3.Studentdbhelper.java package com.example.ganesha.myapplication;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class StudentDbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "STUDENTINFO.DB";
private static final int DATABASE_VERSION = 1;
private static final String CREATE_QUERY="CREATE TABLE "+
Databasefirst.studentinfo.MESS_DETAILS+"("+Databasefirst.studentinfo.STUDENT_NAME+" TEXT,"+Databasefirst.studentinfo.STUDENT_ROLLNO+" TEXT);";
public StudentDbHelper(Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
Log.e("DATABASE_OPERATION ","Database created");
}
//called when table doesnt exist in the first place
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_QUERY);
Log.e("DATABASE_OPERATION ","Table created");
}
public void addInfo(String name,String number,SQLiteDatabase db)
{
ContentValues contentValues = new ContentValues();
contentValues.put(Databasefirst.studentinfo.STUDENT_NAME,name);
contentValues.put(Databasefirst.studentinfo.STUDENT_ROLLNO,number);
//contentValues.put(Databasefirst.studentinfo.MESS_NAME,messname);
db.insert(Databasefirst.studentinfo.MESS_DETAILS,"null",contentValues);
Log.e("DATABASE_OPERATION ","row inserted");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
And I am getting the following log message whenever I enter the submit button in the app 07-17 10:27:20.415 2841-2841/com.example.ganesha.myapplication I/art: Not late-enabling -Xcheck:jni (already on) 07-17 10:27:20.506 2841-2841/com.example.ganesha.myapplication W/System: ClassLoader referenced unknown path: /data/app/com.example.ganesha.myapplication-1/lib/x86 07-17 10:27:22.897 2841-2841/com.example.ganesha.myapplication W/System: ClassLoader referenced unknown path: /data/app/com.example.ganesha.myapplication-1/lib/x86 07-17 10:27:24.273 2841-2841/com.example.ganesha.myapplication W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter (android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package- private method in android.graphics.drawable.Drawable 07-17 10:27:24.529 2841-2941/com.example.ganesha.myapplication D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
[ 07-17 10:27:24.540 2841: 2841 D/ ]
HostConnection::get() New Host Connection established 0xaaa55420, tid 2841
[ 07-17 10:27:24.677 2841: 2941 D/ ]
HostConnection::get() New Host Connection established 0xb3fed310, tid 2941
07-17 10:27:24.691 2841-2941/com.example.ganesha.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4 07-17 10:27:24.733 2841-2941/com.example.ganesha.myapplication W/EGL_emulation: eglSurfaceAttrib not implemented 07-17 10:27:24.733 2841-2941/com.example.ganesha.myapplication W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaa031be0, error=EGL_SUCCESS 07-17 10:27:28.059 2841-2847/com.example.ganesha.myapplication W/art: Suspending all threads took: 5.381ms 07-17 10:27:35.287 2841-2841/com.example.ganesha.myapplication W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection 07-17 10:27:37.181 2841-2941/com.example.ganesha.myapplication W/EGL_emulation: eglSurfaceAttrib not implemented 07-17 10:27:37.181 2841-2941/com.example.ganesha.myapplication W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaaa7ff60, error=EGL_SUCCESS 07-17 10:27:39.284 2841-2941/com.example.ganesha.myapplication E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb4017550 07-17 10:27:39.296 2841-2941/com.example.ganesha.myapplication D/OpenGLRenderer: endAllStagingAnimators on 0xaa0a5c00 (ListPopupWindow$DropDownListView) with handle 0xaa37ce10 07-17 10:27:41.790 2841-2841/com.example.ganesha.myapplication D/AndroidRuntime: Shutting down VM
--------- beginning of crash
07-17 10:27:41.790 2841-2841/com.example.ganesha.myapplication E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.ganesha.myapplication, PID: 2841 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: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) 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: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) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference at com.example.ganesha.myapplication.MainActivity.addDetail(MainActivity.java:50) 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: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) 07-17 10:27:44.735 2841-2841/com.example.ganesha.myapplication I/Process: Sending signal. PID: 2841 SIG: 9