2

Building a TODO list app, that save user Edittext data in SQL database but when i click the button it crashes but restarts without saving the inputs from the user with error

Logcat error

0

8-06 23:36:23.018 2904-2904/com.naive.LISTY E/SQLiteLog: (1) table tasks has no column named date
08-06 23:36:23.019 2904-2904/com.naive.LISTY E/SQLiteDatabase: Error inserting date=12/12/12 Description=hello world title=Hello time=12:50
                                                               android.database.sqlite.SQLiteException: table tasks has no column named date (code 1): , while compiling: INSERT INTO tasks(date,Description,title,time) VALUES (?,?,?,?)
                                                                   at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                   at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
                                                                   at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
                                                                   at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                   at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                   at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                                   at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
                                                                   at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
                                                                   at listy.naive.com.listy.Dialog.Add_task(Dialog.java:63)
                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                   at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
                                                                   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)

on click method

public void Add_task(View view) {

    EditText mTitle = (EditText)findViewById(R.id.title_field);
    String Title = String.valueOf(mTitle.getText());

     EditText mTask = (EditText)findViewById(R.id.description_field);
     String Task = String.valueOf(mTask.getText());

    EditText mDate = (EditText) findViewById(R.id.date_field);
    String Date = String.valueOf(mDate.getText());

    EditText mTime = (EditText) findViewById(R.id.time_field);
    String Time = String.valueOf(mTime.getText());

    SQLiteDatabase db = mHelper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(TaskContract.TaskEntry.COL_TASK_TITLE, Title);
    values.put(TaskContract.TaskEntry.COL_TASK_DESCRIPTION, Task);
    values.put(TaskContract.TaskEntry.COL_TASK_DATE, Date);
    values.put(TaskContract.TaskEntry.COL_TASK_TIME, Time);

    db.insert(TaskContract.TaskEntry.TABLE, null, values);
    //updateUI();
    //db.close();
}

Cursor code

private Cursor updateUI() {
    ArrayList<String> taskList = new ArrayList<>();
    SQLiteDatabase db = mHelper.getReadableDatabase();
    Cursor cursor = db.query(TaskContract.TaskEntry.TABLE,
            new String[]{TaskContract.TaskEntry._ID, TaskContract.TaskEntry.COL_TASK_TITLE, TaskContract.TaskEntry.COL_TASK_DESCRIPTION, TaskContract.TaskEntry.COL_TASK_DATE, TaskContract.TaskEntry.COL_TASK_TIME},
            null, null, null, null, null);
    return cursor;}

mHelper

public class TaskDbHelper extends SQLiteOpenHelper {

    public TaskDbHelper(Context context) {
        super(context, TaskContract.DB_NAME, null, TaskContract.DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTable = "CREATE TABLE " + TaskContract.TaskEntry.TABLE + " ( " +
                TaskContract.TaskEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                TaskContract.TaskEntry.COL_TASK_TITLE + " TEXT NOT NULL," +
                TaskContract.TaskEntry.COL_TASK_DATE + "TEXT NOT NULL," +
                TaskContract.TaskEntry.COL_TASK_DESCRIPTION + "TEXT NOT NULL," +
                TaskContract.TaskEntry.COL_TASK_TIME + "TEXT NOT NULL; )";

        db.execSQL(createTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TaskContract.TaskEntry.TABLE);
        onCreate(db);
    }}
Ved Sarkar
  • 313
  • 1
  • 5
  • 17
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – frogatto Aug 05 '16 at 17:22
  • @VedSarkar did you try my answer? – Vucko Aug 05 '16 at 18:31
  • @Vucko i tried it. Not Crashing but giving an error – Ved Sarkar Aug 06 '16 at 12:37
  • I don't understand. This means that the original error is fixed? A new one is happening now? – Vucko Aug 06 '16 at 15:50
  • Yes a new error now please refer to the edited Logcat error, added mHelper = new TaskDbHelper(this); in Dialog.java onCreate method even tried to add the save line of code in onClick event – Ved Sarkar Aug 06 '16 at 18:12

2 Answers2

0

I think mHelper is null. I think you didn't initialize mHelper.

Rohan Arora
  • 651
  • 5
  • 15
Shubham
  • 482
  • 4
  • 10
0

This line is causing NullPointerException:

SQLiteDatabase db = mHelper.getWritableDatabase();

You have not initialized mHelper anywhere, and thus you're getting the exception. You need to add:

MySQLiteOpenHelper mHelper = new MySQLiteOpenHelper(parameters);

Where MySQLiteOpenHelper is a class of yours which extends SQLiteOpenHelper and overrides some of its methods that you need.

EDIT: After you editing the question with your implementation of SQLiteOpenHelper, do this:

TaskDbHelper mHelper = new TaskDbHelper(this);

If you're in activity pass this, and in fragment, pass getActivity() as the parameter.

Vucko
  • 7,000
  • 2
  • 21
  • 42
  • really new to android, how can i pass getActivity() – Ved Sarkar Aug 06 '16 at 18:07
  • In fragment, type `getActivity()` to get the activity that it's attached to. Pass that as the parameter of `TaskDbHelper(getActivity())` like this. As for the second problem, seems that you have no column named **date**, but since I fixed your original problem, I'd appreciate an upvote and/or accept. Search for the solutions of your new problem, if you cannot find the solution, post a new question, please. – Vucko Aug 06 '16 at 18:14