Reputation: 21
I am trying to get a phone book working on Android which uses an SQLite database.
The thing I am struggling with, is when I click on my next button I want the next row of data in the database to be brought up. However I keep getting this error when I run the application
03-10 12:05:22.455: E/AndroidRuntime(772): FATAL EXCEPTION: main 03-10 12:05:22.455: E/AndroidRuntime(772): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.adressbooktake2/com.example.adressbooktake2.MainActivity}: java.lang.NullPointerException
Here is the code I am using,
MainActivity.Java
public class MainActivity extends Activity {
DBAdaptor db = new DBAdaptor(this);
int count = 1;
Cursor cursor = db.getallRecord();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayRecord(cursor);
}
public void Nextdata (View view){
if (cursor.moveToNext())
{
DisplayRecord(cursor);
}
}
public void Previousdata (View view){
if (cursor.moveToPrevious())
{
DisplayRecord(cursor);
}
}
public void DisplayRecord(Cursor c) {
EditText nameTxt = (EditText)findViewById(R.id.Name);
EditText phoneTxt = (EditText)findViewById(R.id.Phone);
EditText emailTxt = (EditText)findViewById(R.id.Email);
nameTxt.setText(c.getString(1));
phoneTxt.setText(c.getString(2));
emailTxt.setText(c.getString(3));//
}
DBAdaptor.Java
package com.example.adressbooktake2;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdaptor {
public static final String KEY_ROWID = "id";
public static final String KEY_NAME = "name";
public static final String KEY_PHONENUMBER = "phone";
public static final String KEY_EMAIL = "email";
private static final String TAG = "DBAdaptor";
public static final String DATABASE_NAME = "AddressBookDataBase1";
public static final String DATABASE_TABLE = "AddressBook";
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_CREATE = "create table if not exists AddressBook (id integer primary key autoincrement, "
+ "name VARCHAR, phone VARCHAR, email VARCHAR);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdaptor(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try
{
db.execSQL(DATABASE_CREATE);
}
catch (SQLException e)
{
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to" + newVersion +
", which will destroy all old data");
db.execSQL("Drop TABLE IF EXISTS AddressBook");
onCreate(db);
}
}
//--counts rows in database--
public int rowCounter()
{
Cursor mCount= db.rawQuery("select count(*) from AddressBook where id=" + KEY_ROWID, null);
mCount.moveToFirst();
int rowCount= mCount.getInt(0);
return rowCount;
}
//--opens the database---
public DBAdaptor open() throws SQLException
{
db = DBHelper.getWritableDatabase(); //Line 88
return this;
}
//--close the dabatabase
public void close()
{
DBHelper.close();
}
//--insert a record into database--
public long insertRecord(String name, String email, String phone)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_EMAIL, email);
initialValues.put(KEY_PHONENUMBER, phone);
return db.insert(DATABASE_TABLE, null, initialValues);
}
//--deletes a record
public void deleteRecord(long rowId)
{
db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null);
}
//--retrieves a particular record--
public Cursor getRecord(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,
KEY_EMAIL, KEY_PHONENUMBER}, KEY_ROWID + "=" + rowId, null, null, null, null, null);
if (mCursor !=null) {
mCursor.moveToFirst();
}
return mCursor;
}
//--Retrieves all data--
public Cursor getallRecord() throws SQLException
{
{
Cursor gaCursor = db.rawQuery("SELECT* FROM " + DATABASE_TABLE, null);
if (gaCursor !=null)
{
gaCursor.moveToFirst();
}
return gaCursor;
}
}
}
Any help that could be given would be much appreciated if you can see where I am going wrong.
The full stack trace of the exception is(2),
03-10 13:07:39.579: E/AndroidRuntime(770): FATAL EXCEPTION: main 03-10 13:07:39.579: E/AndroidRuntime(770): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.adressbooktake2/com.example.adressbooktake2.MainActivity}: java.lang.NullPointerException 03-10 13:07:39.579: E/AndroidRuntime(770): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106) 03-10 13:07:39.579: E/AndroidRuntime(770): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 03-10 13:07:39.579: E/AndroidRuntime(770): at android.app.ActivityThread.access$600(ActivityThread.java:141) 03-10 13:07:39.579: E/AndroidRuntime(770): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 03-10 13:07:39.579: E/AndroidRuntime(770): at android.os.Handler.dispatchMessage(Handler.java:99) 03-10 13:07:39.579: E/AndroidRuntime(770): at android.os.Looper.loop(Looper.java:137) 03-10 13:07:39.579: E/AndroidRuntime(770): at android.app.ActivityThread.main(ActivityThread.java:5039) 03-10 13:07:39.579: E/AndroidRuntime(770): at java.lang.reflect.Method.invokeNative(Native Method) 03-10 13:07:39.579: E/AndroidRuntime(770): at java.lang.reflect.Method.invoke(Method.java:511) 03-10 13:07:39.579: E/AndroidRuntime(770): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 03-10 13:07:39.579: E/AndroidRuntime(770): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 03-10 13:07:39.579: E/AndroidRuntime(770): at dalvik.system.NativeStart.main(Native Method) 03-10 13:07:39.579: E/AndroidRuntime(770): Caused by: java.lang.NullPointerException 03-10 13:07:39.579: E/AndroidRuntime(770): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:229) 03-10 13:07:39.579: E/AndroidRuntime(770): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224) 03-10 13:07:39.579: E/AndroidRuntime(770): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164) 03-10 13:07:39.579: E/AndroidRuntime(770): at com.example.adressbooktake2.DBAdaptor.open(DBAdaptor.java:88) 03-10 13:07:39.579: E/AndroidRuntime(770): at com.example.adressbooktake2.MainActivity.(MainActivity.java:20) 03-10 13:07:39.579: E/AndroidRuntime(770): at java.lang.Class.newInstanceImpl(Native Method) 03-10 13:07:39.579: E/AndroidRuntime(770): at java.lang.Class.newInstance(Class.java:1319) 03-10 13:07:39.579: E/AndroidRuntime(770): at android.app.Instrumentation.newActivity(Instrumentation.java:1054) 03-10 13:07:39.579: E/AndroidRuntime(770): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
Upvotes: 1
Views: 4925
Reputation: 1308
It's often a source for bugs to do logic before onCreate()
is invoked. In your case:
DBAdaptor db = new DBAdaptor(this);
int count = 1;
Cursor cursor = db.getallRecord();
Try to only declare the variables and initialize them in onCreate
:
public class MainActivity extends Activity {
DBAdaptor db;
int count = 1;
Cursor cursor;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DBAdaptor(this);
cursor = db.getallRecord();
DisplayRecord(cursor);
}
[...]
}
Also take a look at this SO question
Upvotes: 1
Reputation: 19284
if (gaCursor !=null)
{
gaCursor.moveToFirst();
}
else
{
gaCursor.close();
}
Will throw NPE if gaCursor
is null.
If it is null, you cant (and dont need to) close it.
Just remove the else
part.
You also need to change DisplayRecord
function. check if c
is null before calling c.toString()
.
if (c!= null){
nameTxt.setText(c.getString(1));
phoneTxt.setText(c.getString(2));
emailTxt.setText(c.getString(3));//
}
Also, you don't instantiate db
in your DBAdaptor.Java file, Wither use open()
or add this.db = db
inside onCreate
( using open
is better)
Upvotes: 1
Reputation: 473
Seems to be that the Cursor
object returned from getallRecord()
is null...
And the Null Pointer Exception refers to the null Cursor
object accessed in DisplayRecord()
...
Perform a check before displayin' the records as follows :
if(c != null)
//Display Records
else
Log.e(TAG, "Cursor is Null");
Also it doesn't seem to be the right manner in which contacts are to be retrieved from the database...
There are certain ContentProvider
's created to make the data in the database available to an application...This might be of some help...
Upvotes: 0
Reputation: 2928
Move this
EditText nameTxt = (EditText)findViewById(R.id.Name);
EditText phoneTxt = (EditText)findViewById(R.id.Phone);
EditText emailTxt = (EditText)findViewById(R.id.Email);
like this
setContentView(R.layout.activity_main);
EditText nameTxt = (EditText)findViewById(R.id.Name);
EditText phoneTxt = (EditText)findViewById(R.id.Phone);
EditText emailTxt = (EditText)findViewById(R.id.Email);
DisplayRecord(cursor);
Upvotes: 0