ashish verma ashish verma - 6 months ago 23
Java Question

Code Error: AndroidRuntime:FATAL EXCEPTION:main

my code is:
AddContact.java :

package com.example.averm.contactmanager;

import android.app.Activity;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AddContact extends Activity {

private EditText ed_name, ed_number, ed_email;
private Button btn_add;

DatabaseHelper myDb;
String name ="";
String number="";
String email="";

private static final String email_Pattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";

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

ed_name = (EditText)findViewById(R.id.editText);
ed_number = (EditText)findViewById(R.id.editText2);
ed_email = (EditText)findViewById(R.id.editText3);

btn_add = (Button)findViewById(R.id.button5);



ed_name.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
btn_add.setEnabled(!ed_name.getText().toString().trim().isEmpty());
}

@Override
public void afterTextChanged(Editable s) {

}
});
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
name = ed_name.getText().toString();
number =ed_number.getText().toString();
email = ed_email.getText().toString();
// insertData();
if (name.equals("")){
Toast.makeText(getApplicationContext(), "Please Enter Name!",Toast.LENGTH_LONG).show();
}else {
if (number.length() <6){
Toast.makeText(getApplicationContext(), "Please Enter Valid Number!",Toast.LENGTH_LONG).show();
}else {
if (!email.matches(email_Pattern)){
Toast.makeText(getApplicationContext(), "Please Enter Valid Email Address!",Toast.LENGTH_LONG).show();
}else {
insertData();
}
}
}
}
});

}

private void insertData(){
boolean isInserted = myDb.addData(name,Integer.parseInt(number), email);
if (isInserted){
Toast.makeText(getApplicationContext(), ed_name+ " is saved in your Contact List",Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getApplicationContext(), "Data Not Inserted",Toast.LENGTH_LONG).show();
}

}
}


And DatabaseHelpler.java :

package com.example.averm.contactmanager;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

/**
* Created by AVerm on 06-May-16.
*/
public class DatabaseHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "MYCONTACTS";
private static final String TABLE_NAME = "CONTACT";
private static final int DATABASE_VERSION = 1;
private static final String COL_ID = "ID";
private static final String COL_NAME = "NAME";
private static final String COL_PHONE_NUMBER = "NUMBER";
private static final String COL_EMAIL = "EMAIL";
private static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+" ("+COL_ID+" INTEGER PRIMARY KEY AUTOINCREMENT), "+COL_NAME+" VARCHAR(255), "+COL_PHONE_NUMBER+" INTEGER, "+COL_EMAIL+" VARCHAR(255)";
private static final String DROP_TABLE = "DROP TABLE IF EXISTS "+TABLE_NAME;

public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Toast.makeText(context, "Database Created", Toast.LENGTH_LONG).show();
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DROP_TABLE);
onCreate(db);
}

public boolean addData(String name, int number, String email){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COL_NAME, name);
cv.put(COL_PHONE_NUMBER, number);
cv.put(COL_EMAIL, email);

long result = db.insert(TABLE_NAME, null, cv );
if (result == -1) {
return false;
}
else {
return true;
}
}

}


I am Getting this Error while inserting Data in Database, please help!!

05-06 21:26:13.509 10751-10751/com.example.averm.contactmanager E/AndroidRuntime:

FATAL EXCEPTION: main
Process: com.example.averm.contactmanager, PID: 10751
java.lang.NullPointerException: Attempt to invoke virtual method boolean
com.example.averm.contactmanager.DatabaseHelper.addData(java.lang.String, int, java.lang.String)' on a null object reference
at com.example.averm.contactmanager.AddContact.insertData(AddContact.java:82)
at com.example.averm.contactmanager.AddContact.access$400(AddContact.java:15)
at com.example.averm.contactmanager.AddContact$2.onClick(AddContact.java:72)
at android.view.View.performClick(View.java:4781)
at android.view.View$PerformClick.run(View.java:19874)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5268)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)

Answer

You didn't initialise mydb instance inside the method insertData() . Please initialise it using new DatabaseHelper() .

  myDb = new DatabaseHelper();

In insertData() before calling mydb.addData().

Comments