Hi am new to android and learning how to insert data to sqllite database.
Here is my classes.
DB helper Class
package lk.db.learn.databsetesting.data;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
* Created by administrator on 7/6/15.
*/
public class DBHelper extends SQLiteOpenHelper {
public static final String TAG = "DBHelper";
// columns of the schedule table
public static final String TABLE_SCHEDULE= "schedule";
public static final String COLUMN_SCHEDULE_ID = "company_name";
public static final String COLUMN_SCHEDULE_NAME = "company_name";
public static final String COLUMN_SCHEDULE_DATE = "address";
// columns of the items table
public static final String TABLE_ITEM= "items";
public static final String COLUMN_ITEM_ID = "item_id";
public static final String COLUMN_ITEM_VISIT_ID = "visit_id";
public static final String COLUMN_ITEM_NAME = "item_name";
public static final String COLUMN_ITEM_CHECK_STATUS= "item_check_status";
public static final String COLUMN_ITEM_COMMENT = "item_comment";
// columns of the employees table
public static final String TABLE_VISITS = "visits";
public static final String COLUMN_VISITS_ID = "visit_id";
public static final String COLUMN_VISITS_SCHEDULE_ID = "schedule_id";
public static final String COLUMN_VISITS_NAME = "visit_name";
public static final String COLUMN_VISITS_TIME = "visit_time";
public static final String COLUMN_VISITS_PLACE = "visit_place";
public static final String COLUMN_VISITS_ADDRESS ="visit_address";
public static final String COLUMN_VISITS_LOCATION_LAT = "visit_location_lat";
public static final String COLUMN_VISITS_LOCATION_LNG = "visit_location_lng";
public static final String COLUMN_VISITS_STATUS = "visit_status";
private static final String DATABASE_NAME = "certisagent";
private static final int DATABASE_VERSION = 1;
// SQL statement of the visits table creation
private static final String SQL_CREATE_TABLE_VISITS = "CREATE TABLE " + TABLE_VISITS + "("
+ COLUMN_VISITS_ID + " INTEGER PRIMARY KEY, "
+ COLUMN_VISITS_SCHEDULE_ID + " INTEGER, "
+ COLUMN_VISITS_NAME + " TEXT NOT NULL, "
+ COLUMN_VISITS_TIME + " TEXT NOT NULL, "
+ COLUMN_VISITS_PLACE + " TEXT NOT NULL, "
+ COLUMN_VISITS_ADDRESS + " TEXT NOT NULL, "
+ COLUMN_VISITS_LOCATION_LAT + " TEXT NOT NULL, "
+ COLUMN_VISITS_LOCATION_LNG + " TEXT NOT NULL, "
+ COLUMN_VISITS_STATUS + " INTEGER "
+");";
// SQL statement of the schedule table creation
private static final String SQL_CREATE_TABLE_SCHEDULE = "CREATE TABLE " + TABLE_SCHEDULE + "("
+ COLUMN_SCHEDULE_ID + " INTEGER PRIMARY KEY, "
+ COLUMN_SCHEDULE_NAME + " TEXT NOT NULL, "
+ COLUMN_SCHEDULE_DATE + " TEXT NOT NULL "
+");";
// SQL statement of the item table creation
private static final String SQL_CREATE_TABLE_ITEMS = "CREATE TABLE " + TABLE_ITEM + "("
+ COLUMN_ITEM_ID + " INTEGER PRIMARY KEY, "
+ COLUMN_ITEM_VISIT_ID + " INTEGER, "
+ COLUMN_ITEM_NAME + " TEXT NOT NULL, "
+ COLUMN_ITEM_CHECK_STATUS + " INTEGER, "
+ COLUMN_ITEM_COMMENT + " TEXT NOT NULL "
+");";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(SQL_CREATE_TABLE_SCHEDULE);
database.execSQL(SQL_CREATE_TABLE_VISITS);
database.execSQL(SQL_CREATE_TABLE_ITEMS);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG,
"Upgrading the database from version " + oldVersion + " to " + newVersion);
// clear all data
db.execSQL("DROP TABLE IF EXISTS " + TABLE_SCHEDULE);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_VISITS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ITEM);
// recreate the tables
onCreate(db);
}
}
ScheduleDAO Class
package lk.db.learn.databsetesting.data;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import lk.db.learn.databsetesting.model.Schedule;
/**
* Created by administrator on 7/6/15.
*/
public class ScheduleDAO {
public static final String TAG = "ScheduleDAO";
// Database fields
private SQLiteDatabase mDatabase;
private DBHelper mDbHelper;
private Context mContext;
private String[] mAllColumns = {
DBHelper.COLUMN_SCHEDULE_ID,
DBHelper.COLUMN_SCHEDULE_NAME,
DBHelper.COLUMN_SCHEDULE_DATE };
public ScheduleDAO(Context context) {
this.mContext = context;
mDbHelper = new DBHelper(context);
// open the database
try {
open();
} catch (SQLException e) {
Log.e(TAG, "SQLException on openning database " + e.getMessage());
e.printStackTrace();
}
}
public void open() throws SQLException {
mDatabase = mDbHelper.getWritableDatabase();
}
public void close() {
mDbHelper.close();
}
public Schedule createschedule(int id, String name, String date) {
ContentValues values = new ContentValues();
values.put(DBHelper.COLUMN_SCHEDULE_ID, id);
values.put(DBHelper.COLUMN_SCHEDULE_NAME, name);
values.put(DBHelper.COLUMN_SCHEDULE_DATE, date);
long insertId = mDatabase
.insert(DBHelper.TABLE_SCHEDULE, null, values);
Cursor cursor = mDatabase.query(DBHelper.TABLE_SCHEDULE, mAllColumns,
DBHelper.COLUMN_SCHEDULE_ID + " = " + insertId, null, null,
null, null);
cursor.moveToFirst();
Schedule newschedule = cursorToSchedule(cursor);
cursor.close();
return newschedule;
}
// public void deleteSchedule(Schedule schedule) {
// long id = schedule.getId();
// // delete all employees of this company
// ScheduleDAO scheduledao = new ScheduleDAO(mContext);
//
//
// EmployeeDAO employeeDao = new EmployeeDAO(mContext);
// List<Employee> listEmployees = employeeDao.getEmployeesOfCompany(id);
// if (listEmployees != null && !listEmployees.isEmpty()) {
// for (Employee e : listEmployees) {
// employeeDao.deleteEmployee(e);
// }
// }
//
// System.out.println("the deleted company has the id: " + id);
// mDatabase.delete(DBHelper.TABLE_COMPANIES, DBHelper.COLUMN_COMPANY_ID
// + " = " + id, null);
// }
public List<Schedule> getAllschedules() {
List<Schedule> listSchedules = new ArrayList<Schedule>();
Cursor cursor = mDatabase.query(DBHelper.TABLE_SCHEDULE, mAllColumns,
null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Schedule schedule = cursorToSchedule(cursor);
listSchedules.add(schedule);
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
}
return listSchedules;
}
public Schedule getScheduleById(int id) {
Cursor cursor = mDatabase.query(DBHelper.TABLE_SCHEDULE, mAllColumns,
DBHelper.COLUMN_SCHEDULE_ID + " = ?",
new String[] { String.valueOf(id) }, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
Schedule schedule = cursorToSchedule(cursor);
return schedule;
}
protected Schedule cursorToSchedule(Cursor cursor) {
Schedule schedule = new Schedule();
schedule.setId(cursor.getInt(0));
schedule.setName(cursor.getString(1));
schedule.setDate(cursor.getString(2));
return schedule;
}
}
Schedule class
package lk.db.learn.databsetesting.model;
import java.io.Serializable;
/**
* Created by administrator on 7/6/15.
*/
public class Schedule implements Serializable {
public static final String TAG = "Employee";
private static final long serialVersionUID = -7406082437623008161L;
private int sId;
private String sName;
private String SDate;
public Schedule() {}
public Schedule(int id, String name, String date) {
this.sId = id;
this.sName = name;
this.SDate = date;
}
public long getId() {
return sId;
}
public void setId(int sId) {
this.sId = sId;
}
public String getName() {
return sName;
}
public void setName(String sName) {
this.sName = sName;
}
public String getDate() {
return SDate;
}
public void setDate(String SDate) {
this.SDate = SDate;
}
}
Main Activity
package lk.db.learn.databsetesting;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import lk.db.learn.databsetesting.data.ScheduleDAO;
import lk.db.learn.databsetesting.model.Schedule;
public class MainActivity extends ActionBarActivity {
private ScheduleDAO mscheduleDAo;
public int schedule_id = 1;
public String schedule_name = "Schedule 1";
public String date = "2015-07-06";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Schedule createdschedule = mscheduleDAo.createschedule(Integer.parseInt(String.valueOf(schedule_id)), schedule_name, date);
Toast.makeText(this, (CharSequence) createdschedule, Toast.LENGTH_LONG).show();
this.mscheduleDAo = new ScheduleDAO(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Eclipse don't show any errors but i run the code in emulator, i get the following error.
07-06 12:08:30.127 27711-27711/lk.db.learn.databsetesting E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: lk.db.learn.databsetesting, PID: 27711
java.lang.RuntimeException: Unable to start activity ComponentInfo{lk.db.learn.databsetesting/lk.db.learn.databsetesting.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.access$800(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at lk.db.learn.databsetesting.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5442)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.access$800(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Can some one help me to fix this . Tnx.
Aucun commentaire:
Enregistrer un commentaire